import { Tip , Confirm , Router , Statistic , proxy , ajax , jsonp , Storage } from './modules/index' ; import URI from '@utils/uri' ; import { EMPTY_FUNCTION } from '../constant' ; import { AjaxOptions , JsonpOptions , TipOptions , ConfirmOptions } from '../interface' ; const extend = ( destination , source ) => { for ( let key in source ) { if ( source . hasOwnProperty ( key ) ) { destination [ key ] = source [ key ] ; } } return destination ; } ; function App ( data , watchs ) { this . data = data ; this . watchs = watchs ; this . $router = Router ; this . $route = URI . parse ( window . location . href ) ; this . $statistic = Statistic ; this . $storage = new Storage ( ) ; } App . prototype = { bindEvents : EMPTY_FUNCTION , init : EMPTY_FUNCTION , $tip : function ( options ) : void { new Tip ( options ) ; } , $ajax : function ( options ) : Promise < any > | string { return ajax ( options ) ; } , $jsonp : function ( options ) : Promise < any > { return jsonp ( options ) ; } , $confirm : function ( options ) { return new Confirm ( options ) ; } , _init : function ( ) : void { this . bindEvents ( ) ; if ( this . data ) { for ( let key in this . data ) { Object . defineProperty ( this , key , { get ( ) { return this . data [ key ] ; } , set ( value ) { this . data [ key ] = value ; } } ) ; } } if ( this . watchs ) { for ( let key in this . watchs ) { Object . defineProperty ( this , key , { get ( ) { return this . watchs [ key ] ; } , set ( value ) { this . watchs [ key ] = value ; } } ) ; proxy ( { data : this . watchs , key : key , callback : this [ `${ key }Handler` ] . bind ( this ) } ) ; } } this . init ( ) ; } } ; App . extends = ( ... args ) => { const Super = App ; const Sub = function ( data , watchs ) { App . call ( this , data , watchs ) ; this . _init ( ) ; } ; Sub . prototype = Object . create ( Super . prototype ) ; Sub . prototype . constructor = Sub ; [ ... args ] . map ( ( item ) => { extend ( Sub . prototype , item ) ; } ) ; return Sub ; } ; export default App ;