all files / dui/src/core/ plugin.js

100% Statements 28/28
71.43% Branches 10/14
87.5% Functions 7/8
100% Lines 28/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                                                16×   16×   16×   16×   16×   16×                                                                
/**
 * 插件核心构造函数
 *
 * @file plugin核心
 * @author Brian Li(lbxxlht@163.com)
 */
var define = typeof define === 'function' && define.amd ? define : function (factory) {
    typeof module === 'object' ? (module.exports = factory(require)) : '';
};
 
define(function (require) {
 
    var util = require('./util');
 
    // public protected/////////////////////////////////////////////////////////////////
 
    // required依赖说明:
    // (1)依赖不存在,本尊不能工作,不能初始化,报ERROR;
    // (2)依赖报ERROR,本尊不能工作,不能初始化,本尊报ERROR。
    // (3)依赖存在,等依赖初始化完毕,本尊才能初始化,初始化顺序由用户指定
 
    /**
     * 插件原型
     *
     * @constructor
     * @param {Object} param 初始化对象
     */
    function Plugin(param) {
        // 宿主对象,即UI实例
        this.host = null;
        // 依赖名称,必须与UI初始化param中plugin对象中对应的键值相同
        this.name = util.getFunctionName(this.constructor);
        // 是否加载完毕
        this.isLoaded = false;
        // 是否加载失败
        this.isError = false;
        // 是否正在工作
        this.isWorking = false;
        // 事件hash,用于on和fire
        this._events = {};
    }
 
    /**
     * 绑定事件
     *
     * @param {string} type 事件类型
     * @param {Function} callback 事件触发时的回调
     * @param {Object | null} me callback绑定的上下位
     */
    Plugin.prototype.on = function (type, callback, me) {
        if (me === undefined) {
            me = this;
        }
        callback = util.bind(callback, me);
        this._events[type] = this._events[type] || [];
        this._events[type].push(callback);
    };
 
    /**
     * 触发事件
     *
     * @param {string} type 事件类型
     * @param {Object | null} param callback的回传
     */
    Plugin.prototype.fire = function (type, param) {
        param = param || {};
        param.type = type;
        param.target = this.host;
        Eif (this._events[type] instanceof Array) {
            for (var n = 0; n < this._events[type].length; n++) {
                this._events[type][n](param);
            }
        }
    };
 
    // 装载
    Plugin.prototype.load = function () {};
 
    // 宿主渲染完成后回调
    Plugin.prototype.hostRendered = function () {};
 
    // 卸载
    Plugin.prototype.unload = function () {};
 
    // private //////////////////////////////////////
 
    return Plugin;
});