引用JS写的一个事件的分发中心

/*
 * @create time 2015-03-24
 * @author Tong_yx
 * @desc 全局事件处理中心
 */

/**
 * 事件对象
 * @param {string} token 事件的token值,用于删除事件
 * @param {object} obj 事件的对象
 * @param {string} funcname 函数名称
 * @param {object} target 目标对象
 * @constructor
 */
function CallBackFunc(token, obj, funcname, target) {
    this._obj = obj;
    this._funcname = funcname;
    this._token = token;
    this._target = target;
};

CallBackFunc.prototype.Execute = function() {
    if (this._obj) {
        var func = this._obj[this._funcname];
        if (func) {
            func.apply(this._obj, arguments);
        }
    } else {
        this._funcname.apply(null, arguments);
    }

};

CallBackFunc.prototype.CheckTarget = function(target) {
    return this._target == target ? true : false;
};

CallBackFunc.prototype.HasTarget = function() {
    return this._target;
};

CallBackFunc.prototype.IsValid = function() {
    return typeof this._funcname == "function" || (this._obj && typeof this._funcname == "string");
};

var EventCenter = function() {
    /**
     * 事件对象存储
     * @type {{}}
     * @private
     */
    this._EventHandlerMap = {};
    /**
     * 待处理异步事件列表
     * @type {array}
     * @private
     */
    this._EventList = [];
    this._TimerID = null;
    this._subUid = 0;
    /**
     * 是否需要先进行事件注册才能进行事件的绑定
     * @type {boolean}
     * @private
     */
    //this._m_bMustReg = false;
    this._m_bMustReg = true;
}
/**
 * 设置是否需要先行注册才能绑定事件
 * @param {boolean} bEnable 是否启用该功能
 */
EventCenter.prototype.setMustRegMode = function (bEnable){
    this._m_bMustReg = (bEnable)?true:false;
}
/**
 * 返回是否能注册事件
 * @param {string} id 事件id
 * @param {object} target 对象绑定
 * @returns {boolean | object}
 * @constructor
 */
EventCenter.prototype.CanRegEvent = function (id,target)
{
    var handler = this.FindEventHandler(id,target);
    if(this._m_bMustReg) {
        return (handler)?handler:false;
    }
    return (handler)?handler:true;
};
/**
 * 取得事件的Handler
 * @param {string} id
 * @param {object} target
 * @returns {boolean} 返回找到的Handler,若Handler不存在,则返回是否注册过该事件
 * @constructor
 */
EventCenter.prototype.FindEventHandler = function(id, target) {
    var handler = this._EventHandlerMap[id];
    return (handler)?handler:this._EventHandlerMap.hasOwnProperty(id);
};

/**
 * 注册事件
 * @param {string} id 事件id
 * @constructor
 */
EventCenter.prototype.RegisterEvent = function (id)
{
    if(id)
    {
        if(id.constructor == Array)
        {
            for(var i=0;i 0) {
        this._EventList.push(arguments);
        if (!this._TimerID) {
            this._TimerID = setTimeout(this.OnUpdate, 500, this);
        }
    }
};

EventCenter.prototype.raise = EventCenter.prototype.RaiseEvent;
EventCenter.prototype.raiseByTarget = EventCenter.prototype.RaiseEventByTarget;
EventCenter.prototype.emit = EventCenter.prototype.RaiseEvent;

EventCenter.prototype.post = EventCenter.prototype.PostEvent;
//心跳函数
EventCenter.prototype.OnUpdate = function(THIS) {
    var len = THIS._EventList.length;
    for (var i = 0; i < len; ++i) {
        var args = THIS._EventList[i];
        THIS.RaiseEvent.apply(THIS, args);
        delete THIS._EventList[i];
    }
    THIS._EventList.splice(0, len);
    if (THIS._EventList.length) {
        THIS._TimerID = setTimeout(THIS.OnUpdate, 500, THIS);
    } else {
        clearTimeout(THIS._TimerID);
        THIS._TimerID = null;
    }
};

var eventCenter = null;

EventCenter.GetInstance = function() {
    if (!eventCenter) {
        eventCenter = new EventCenter();
    }
    return eventCenter;
};
EventCenter.GetInstance();

你可能感兴趣的:(javaScript)