src/js/zuix/ContextController.js
function ContextController(context) {
    const _t = this;

    this._view = null;

    this.context = context;

    /**
     * @package
     * @type {!Array.<ZxQuery>}
     **/
    this._fieldCache = [];

    // Interface methods

    /** @type {function} */
    this.init = null;
    /** @type {function} */
    this.create = null;
    /** @type {function} */
    this.update = null;
    /** @type {function} */
    this.destroy = null;

    /**
     * @protected
     * @type {!Array.<Element>}
     * */
    this._childNodes = [];
    /** @type {function} */
    this.saveView = function() {
        this.restoreView();
        this.view().children().each(function(i, el) {
            _t._childNodes.push(el);
        });
    };
    this.restoreView = function() {
        if (this._childNodes.length > 0) {
            _t.view().html('');
            z$.each(_t._childNodes, function(i, el) {
                _t.view().append(el);
            });
            this._childNodes.length = 0;
        }
    };

    this.on = function(eventPath, handler) {
        this.addEvent(eventPath, handler);
        return this;
    };
    /** @protected */
    this.mapEvent = function(eventMap, target, eventPath, handler) {
        if (target != null) {
            target.off(eventPath, this.eventRouter);
            eventMap[eventPath] = handler;
            target.on(eventPath, this.eventRouter);
        } else {
            // TODO: should report missing target
        }
    };
    /** @protected */
    this.eventRouter = function(e) {
        const v = _t.view();
        if (typeof context._behaviorMap[e.type] === 'function') {
            context._behaviorMap[e.type].call(v, e, e.detail, v);
        }
        if (typeof context._eventMap[e.type] === 'function') {
            context._eventMap[e.type].call(v, e, e.detail, v);
        }
        // TODO: else-> should report anomaly
    };

    // create event map from context options
    const options = context.options();
    let handler = null;
    if (options.on != null) {
        for (let ep in options.on) {
            if (options.on.hasOwnProperty(ep)) {
                handler = options.on[ep];
                _t.addEvent(ep, handler);
            }
        }
    }
    // create behavior map from context options
    if (options.behavior != null) {
        for (let bp in options.behavior) {
            if (options.behavior.hasOwnProperty(bp)) {
                handler = options.behavior[bp];
                _t.addBehavior(bp, handler);
            }
        }
    }

    context.controller().call(this, this);

    return this;
}
function ContextController(context) {
    const _t = this;

    this._view = null;

    this.context = context;

    /**
     * @package
     * @type {!Array.<ZxQuery>}
     **/
    this._fieldCache = [];

    // Interface methods

    /** @type {function} */
    this.init = null;
    /** @type {function} */
    this.create = null;
    /** @type {function} */
    this.update = null;
    /** @type {function} */
    this.destroy = null;

    /**
     * @protected
     * @type {!Array.<Element>}
     * */
    this._childNodes = [];
    /** @type {function} */
    this.saveView = function() {
        this.restoreView();
        this.view().children().each(function(i, el) {
            _t._childNodes.push(el);
        });
    };
    this.restoreView = function() {
        if (this._childNodes.length > 0) {
            _t.view().html('');
            z$.each(_t._childNodes, function(i, el) {
                _t.view().append(el);
            });
            this._childNodes.length = 0;
        }
    };

    this.on = function(eventPath, handler) {
        this.addEvent(eventPath, handler);
        return this;
    };
    /** @protected */
    this.mapEvent = function(eventMap, target, eventPath, handler) {
        if (target != null) {
            target.off(eventPath, this.eventRouter);
            eventMap[eventPath] = handler;
            target.on(eventPath, this.eventRouter);
        } else {
            // TODO: should report missing target
        }
    };
    /** @protected */
    this.eventRouter = function(e) {
        const v = _t.view();
        if (typeof context._behaviorMap[e.type] === 'function') {
            context._behaviorMap[e.type].call(v, e, e.detail, v);
        }
        if (typeof context._eventMap[e.type] === 'function') {
            context._eventMap[e.type].call(v, e, e.detail, v);
        }
        // TODO: else-> should report anomaly
    };

    // create event map from context options
    const options = context.options();
    let handler = null;
    if (options.on != null) {
        for (let ep in options.on) {
            if (options.on.hasOwnProperty(ep)) {
                handler = options.on[ep];
                _t.addEvent(ep, handler);
            }
        }
    }
    // create behavior map from context options
    if (options.behavior != null) {
        for (let bp in options.behavior) {
            if (options.behavior.hasOwnProperty(bp)) {
                handler = options.behavior[bp];
                _t.addBehavior(bp, handler);
            }
        }
    }

    context.controller().call(this, this);

    return this;
}
function ContextController ( context ) { const _t = this ; this . _view = null ; this . context = context ; /**
     * @package
     * @type {!Array.<ZxQuery>}
     **/ this . _fieldCache = [ ] ; // Interface methods /** @type {function} */ this . init = null ; /** @type {function} */ this . create = null ; /** @type {function} */ this . update = null ; /** @type {function} */ this . destroy = null ; /**
     * @protected
     * @type {!Array.<Element>}
     * */ this . _childNodes = [ ] ; /** @type {function} */ this . saveView = function ( ) { this . restoreView ( ) ; this . view ( ) . children ( ) . each ( function ( i , el ) { _t . _childNodes . push ( el ) ; } ) ; } ; this . restoreView = function ( ) { if ( this . _childNodes . length > 0 ) { _t . view ( ) . html ( '' ) ; z$ . each ( _t . _childNodes , function ( i , el ) { _t . view ( ) . append ( el ) ; } ) ; this . _childNodes . length = 0 ; } } ; this . on = function ( eventPath , handler ) { this . addEvent ( eventPath , handler ) ; return this ; } ; /** @protected */ this . mapEvent = function ( eventMap , target , eventPath , handler ) { if ( target != null ) { target . off ( eventPath , this . eventRouter ) ; eventMap [ eventPath ] = handler ; target . on ( eventPath , this . eventRouter ) ; } else { // TODO: should report missing target } } ; /** @protected */ this . eventRouter = function ( e ) { const v = _t . view ( ) ; if ( typeof context . _behaviorMap [ e . type ] === 'function' ) { context . _behaviorMap [ e . type ] . call ( v , e , e . detail , v ) ; } if ( typeof context . _eventMap [ e . type ] === 'function' ) { context . _eventMap [ e . type ] . call ( v , e , e . detail , v ) ; } // TODO: else-> should report anomaly } ; // create event map from context options const options = context . options ( ) ; let handler = null ; if ( options . on != null ) { for ( let ep in options . on ) { if ( options . on . hasOwnProperty ( ep ) ) { handler = options . on [ ep ] ; _t . addEvent ( ep , handler ) ; } } } // create behavior map from context options if ( options . behavior != null ) { for ( let bp in options . behavior ) { if ( options . behavior . hasOwnProperty ( bp ) ) { handler = options . behavior [ bp ] ; _t . addBehavior ( bp , handler ) ; } } } context . controller ( ) . call ( this , this ) ; return this ; }
ContextController user-defined handlers definition

@typedef {Object} ContextController
@property {function} init Function that gets called after loading and before the component is created.
@property {function} create Function that gets called after loading, when the component is actually created and ready.
@property {function} update Function called when the data model of the component is updated.
@property {function} destroy Function called when the component is destroyed.
 
ContextController constructor.

@param {ComponentContext} context
@return {ContextController}
@constructor
ContextController user - defined handlers definition