Skip to content

Instantly share code, notes, and snippets.

@th3hunt
Created June 5, 2014 10:38
Show Gist options
  • Save th3hunt/7e20085a9b7d0b5f7d52 to your computer and use it in GitHub Desktop.
Save th3hunt/7e20085a9b7d0b5f7d52 to your computer and use it in GitHub Desktop.
backbone.fwd + :target option
// backbone.fwd
// ------------
// Forward events from a source, through a target object
//
// v0.1.0
// Copyright (C)2014 Muted Solutions, LLC.
// Distributed under MIT license
//
// https://github.com/derickbailey/backbone.fwd
// Add a :target option to allow for an 3rd object to act as a bridge between the emitter and the listener
Backbone.fwd = function(source, options){
options = options || {};
this.listenTo(source, "all", function(){
var args = Array.prototype.slice.call(arguments);
var eventName = args.shift();
var target = options.target || this;
// handle prefix for event name
if (options.prefix){
eventName = options.prefix + ":" + eventName;
}
// handle suffix for event name
if (options.suffix){
eventName = eventName + ":" + options.suffix;
}
args.unshift(eventName);
target.trigger.apply(target, args);
}, this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment