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; | |
})(); |
Shouldn't line 7 and 21 be this?
args.unshift(eventName);
As is it's not forwarding the event.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This adds a forwardEvent and proxyEvent method to Backbone Models, Views, Collections, and Routers. The forwardEvent method listens for events triggered on the current object and triggers them on the target. The proxyEvent method listens for events on the source object and triggers them on the current object.