Nodejs has Eventemitter class, think backbone have an event module can do events binding and triggering, is the core module of backbone.
Backbone Event Module
- On Add custom events
- Off Delete custom events
- Trigger Send a custom event
- Once add a custom event that executes only once (internally dependent on _.once)
- Listento Adding an Observation object
- Listentoonce add an Observation object that executes only once
- Stoplistening deleting an added observation object
------------------------------------
On:function (name, callback, context) { if (!eventsapi (this, ' on ', name, [callback, context]) | |!callback) return THI s; this._events | | (this._events = {}); var events = This._events[name] | | (This._events[name] = []); Events.push ({callback:callback, context:context, Ctx:context | | this}); return this; },
The _events attribute is added to the current instance of Bancbone.event, and all time subscribed through backbone.events is found on this property.
_events={ name:[{callback:function () {}, Context:object, Ctx:object}], name:[{callback:function () {}, Context:object, Ctx:object}] }
Trigger:function (name) { if (!this._events) return this; var args = slice.call (arguments, 1); if (!eventsapi (this, ' Trigger ', name, args)] return this; var events = This._events[name]; var allevents = This._events.all; if (events) triggerevents (events, args); if (allevents) triggerevents (allevents, arguments); return this; }
Object {callback:function, context:object, Ctx:object}
var triggerevents = function (events, args) { var ev, i =-1, L = events.length, a1 = Args[0], a2 = args[1], a3 = args[ 2]; Switch (args.length) {case 0:while (++i < L) (ev = Events[i]). Callback.call (ev.ctx); return; Case 1:while (++i < L) (ev = Events[i]). Callback.call (Ev.ctx, A1); return; Case 2:while (++i < L) (ev = Events[i]). Callback.call (Ev.ctx, a1, A2); return; Case 3: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); return; } };
Backbone event Events Subscribe to and publish source code small read