Definefunction(require,exports,module) {varevents=function(){ varArray = []; varPush =Array.push; varSlice =Array.slice; varSplice =Array.splice; varEvents = { //Bind an event to a ' callback ' function. Passing ' all ' ' would bind //The callback to all events fired.Onfunction(name, callback, context) {if(!eventsapi ( This, ' on ', name, [callback, context]) | | !callback)return This; This. _events | | ( This. _events = {}); varEvents = This. _events[name] | | ( This. _events[name] = []); Events.push ({callback:callback, context:context, Ctx:context|| This}); return This; }, //Bind an event to is triggered a single time. after the first time //The callback is invoked, it'll be removed.Oncefunction(name, callback, context) {if(!eventsapi ( This, ' Once ', name, [callback, context]) | | !callback)return This; varSelf = This; varonce = _.once (function() {Self.off (name, once); Callback.apply ( This, arguments); }); Once._callback=callback; return This. On (name, once, context); }, //Remove one or many callbacks. If ' context ' is null, removes all //callbacks with that function. If ' callback ' is null, removes all //callbacks for the event. If ' name ' is null, removes all bound //callbacks for all events.Offfunction(name, callback, context) {varretain, Ev, events, names, I, L, J, K; if(! This. _events | | !eventsapi ( This, ' off ', name, [callback, context])return This; if(!name &&!callback &&!)context) { This. _events = {}; return This; } names= name? [Name]: _.keys ( This. _events); for(i = 0, L = names.length; i < L; i++) {Name=Names[i]; if(Events = This. _events[name]) { This. _events[name] = retain = []; if(Callback | |context) { for(j = 0, k = events.length; J < K; J + +) {EV=Events[j]; if((Callback && callback!== Ev.callback && callback!== ev.callback._callback) | |(Context&& Context!==Ev.context)) {Retain.push (EV); } } } if(!retain.length)Delete This. _events[name]; } } return This; }, //Trigger One or many events, firing all bound callbacks. Callbacks is //passed the same arguments as ' trigger ' is, apart from the event name //(unless you ' re listening in ' "All" ', which'll cause your callback to //Receive the true name of the event as the first argument).Triggerfunction(name) {if(! This. _events)return This; varargs = Slice.call (arguments, 1); if(!eventsapi ( This, ' Trigger ', name, args)return This; varEvents = This. _events[name]; varAllevents = This. _events.all; if(events) triggerevents (events, args); if(allevents) triggerevents (allevents, arguments); return This; }, //Tell this object to stop listening to either specific events ... or //To every object it's currently listening to.Stoplistening:function(obj, name, callback) {varListeners = This. _listeners; if(!listeners)return This; varDeletelistener =!name &&!callback; if(typeofName = = = ' object ') callback = This; if(obj) (listeners = {}) [Obj._listenerid] =obj; for(varIdinchlisteners) {Listeners[id].off (name, callback, This); if(Deletelistener)Delete This. _listeners[id]; } return This; } }; //Regular expression used to split event strings. varEventsplitter =/\s+/; //Implement Fancy features of the Events API such as multiple event //names ' "Change Blur" ' and Jquery-style event maps ' {change:action} ' //In terms of the existing API. varEventsapi =function(obj, action, name, rest) {if(!name)return true; //Handle event maps. if(typeofName = = = ' object ') { for(varKeyinchname) {obj[action].apply (obj, [Key, Name[key]].concat (rest)); } return false; } //Handle Space separated event names. if(eventsplitter.test (name)) {varNames =Name.split (Eventsplitter); for(vari = 0, L = names.length; I < L; i++) {obj[action].apply (obj, [Names[i]].concat (rest)); } return false; } return true; }; //A Difficult-to-believe, but optimized internal dispatch function for //triggering events. Tries to keep the usual cases speedy (most internal //Backbone Events has 3 arguments). varTriggerevents =function(events, args) {varEV, i =-1, L = events.length, a1 = Args[0], a2 = args[1], a3 = args[2]; Switch(args.length) { Case0: while(++i < L) (ev = Events[i]). Callback.call (EV.CTX);return; Case1: while(++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1);return; Case2: while(++i < L) (ev = Events[i]). Callback.call (Ev.ctx, a1, A2);return; Case3: while(++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1, A2, A3);return; default: while(++i < L) (EV =events[i]). callback.apply (Ev.ctx, args); } }; //Aliases for backwards compatibility.Events.bind =Events.on; Events.unbind=Events.off; returnEvents; } () Module.exports=Events})
JavaScript event triggering and receiving source code