Created
August 8, 2012 17:56
-
-
Save chentepixtol/3297087 to your computer and use it in GitHub Desktop.
appEvents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var oldDelegateEvents = Backbone.View.prototype.delegateEvents; | |
| var oldUndelegateEvents = Backbone.View.prototype.undelegateEvents; | |
| var callbacks = {}; | |
| _.extend(Backbone.View.prototype, { | |
| delegateEvents: function(events){ | |
| oldDelegateEvents.call(this, events); | |
| this.globalEvents && this.delegateGlobalEvents(this.globalEvents); | |
| }, | |
| undelegateEvents: function(){ | |
| oldUndelegateEvents.call(this); | |
| this.globalEvents && this.undelegateGlobalEvents(this.globalEvents); | |
| }, | |
| delegateGlobalEvents: function(events) { | |
| var view = this; | |
| for ( event in events ) { | |
| var fnName = events[event]; | |
| var fn = this[fnName]; | |
| if ( fn && _.isFunction(fn) ) { | |
| var wrapperFunction = function(params, target, subject) { | |
| if( !!subject ) { | |
| if ( subject == view.cid ) { | |
| fn.call(view, params, target, subject); | |
| } | |
| } else { | |
| fn.call(view, params, target, subject); | |
| } | |
| }; | |
| callbacks[event + this.cid] = wrapperFunction; | |
| Backbone.Events.on(event, wrapperFunction); | |
| } | |
| } | |
| }, | |
| undelegateGlobalEvents: function(events) { | |
| for ( event in events ) { | |
| Backbone.Events.off(event, callbacks[event + this.cid]); | |
| delete callbacks[event + this.cid]; | |
| } | |
| }, | |
| globalTrigger: function(eventName, params, subject) { | |
| var target = this.cid; | |
| Backbone.Events.trigger(eventName, params, target, subject); | |
| }, | |
| sendEvent: function(view, eventName, params) { | |
| if (view && view.cid) { | |
| this.globalTrigger(eventName, params, view.cid); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment