Created
June 5, 2014 10:38
-
-
Save th3hunt/7e20085a9b7d0b5f7d52 to your computer and use it in GitHub Desktop.
backbone.fwd + :target option
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
// 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