Created
September 8, 2012 04:10
-
-
Save thegrubbsian/3671728 to your computer and use it in GitHub Desktop.
Proxy or forward Backbone events through a mediator
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
(function() { | |
var proxy = function(source, eventName) { | |
var _self = this; | |
source.on(eventName, function(evt) { | |
var args = Array.prototype.slice.apply(arguments).splice(1); | |
args.unshift(evt); | |
_self.trigger.apply(_self, args); | |
}); | |
}; | |
Backbone.View.prototype.proxyEvent = proxy; | |
Backbone.Model.prototype.proxyEvent = proxy; | |
Backbone.Collection.prototype.proxyEvent = proxy; | |
Backbone.Router.prototype.proxyEvent = proxy; | |
var forward = function(target, eventName) { | |
var _self = this; | |
_self.on(eventName, function(evt) { | |
var args = Array.prototype.slice.apply(arguments).splice(1); | |
args.unshift(evt); | |
target.trigger.apply(target, args); | |
}); | |
}; | |
Backbone.View.prototype.forwardEvent = forward; | |
Backbone.Model.prototype.forwardEvent = forward; | |
Backbone.Collection.prototype.forwardEvent = forward; | |
Backbone.Router.prototype.forwardEvent = forward; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't line 7 and 21 be this?
As is it's not forwarding the event.