/**
* 插件核心构造函数
*
* @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;
});
|