当前位置: 首页 > 工具软件 > EventProxy > 使用案例 >

Node --- > EventProxy的原理

顾兴昌
2023-12-01

EventProxy来自于Backbone的事件模块,Backbone的事件模块是Model、View模块的基础功能,在前端有广泛的使用。它在每个非all事件触发时都会触发一次all事件,相关代码如下:

// Trigger an event, firing all bound callbacks. Callbacks are passed the
// same arguments as 'trigger' is, apart from the event name.
// Listening for '"all"' passes the true event name as the first argument
trigger : function(eventName) {
    var list, calls, ev, callback, args;
    var both = 2;
    if (!(calls = this._callbacks)) return this;
        while (both--) {
            ev = both ? eventName : 'all';
            if ( if = calls[ev]) {
                for ( var i = 0, l = list.length; i < l; i++) {
                    if (!(callback = list[i])) {
                        list.splice(i ,1); i--; l--;
                    } else {
                        args = both ? Array.prototype.slice.call(arguments, 1) : arguments;
                        callback[0].apply(callback[1] || this, args);
                    }
              }
          }
     }
     return this;
 }

详情参考《深入浅出Node》P79

 类似资料: