Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created October 7, 2012 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mxriverlynn/3847156 to your computer and use it in GitHub Desktop.
Save mxriverlynn/3847156 to your computer and use it in GitHub Desktop.
javascript mixins
var foo = {
doSomething: function(){
// ...
}
}
var bar = {};
bar.doSomething = foo.doSomething;
var foo = {
baz: function(){
// ...
},
config: [ ... ]
}
var bar = {
config: { ... }
};
// ECMAScript "bind"
bar.baz = foo.baz.bind(foo);
// Undescore "bind"
bar.baz = _.bind(foo.baz, foo);
// ... many more options
Marionette.addEventBinder = function(target){
// create the "source" of the functionality i need
var eventBinder = new Marionette.EventBinder();
// add the methods i need to the target object, binding them correctly
target.bindTo = _.bind(eventBinder.bindTo, eventBinder);
target.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder);
target.unbindAll = _.bind(eventBinder.unbindAll, eventBinder);
};
// use the mixin method
var myApp = new Marionette.Application();
Marionette.addEventBinder(myApp);
// build a mixin function to take a target that receives the mixin,
// a source that is the mixin, and a list of methods / attributes to
// copy over to the target
function mixInto(target, source, methodNames){
// ignore the actual args list and build from arguments so we can
// be sure to get all of the method names
var args = Array.prototype.slice.apply(arguments);
target = args.shift();
source = args.shift();
methodNames = args;
var method;
var length = methodNames.length;
for(var i = 0; i < length; i++){
method = methodNames[i];
// bind the function from the source and assign the
// bound function to the target
target[method] = _.bind(source[method], source);
}
}
// make use of the mixin function
var myApp = new Marionette.Application();
mixInto(myApp, Marionette.EventBinder, "bindTo", "unbindFrom", "unbindAll");
// build a mixin function to take a target that receives the mixin,
// a source that is the mixin, and a list of methods / attributes to
// copy over to the target
function mixInto(target, source, methodNames){
// ignore the actual args list and build from arguments so we can
// be sure to get all of the method names
var args = Array.prototype.slice.apply(arguments);
target = args.shift();
source = args.shift();
methodNames = args;
var method;
var length = methodNames.length;
for(var i = 0; i < length; i++){
method = methodNames[i];
// build a function with a closure around the source
// and forward the method call to the source, passing
// along the method parameters and setting the context
target[method] = function(){
var args = Array.prototype.slice(arguments);
source[method].apply(source, args);
}
}
}
// make use of the mixin function
var myApp = new Marionette.Application();
mixInto(myApp, Marionette.EventBinder, "bindTo", "unbindFrom", "unbindAll");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment