public
Last active

javascript mixins

  • Download Gist
1.js
JavaScript
1 2 3 4 5 6 7 8
var foo = {
doSomething: function(){
// ...
}
}
 
var bar = {};
bar.doSomething = foo.doSomething;
2.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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
3.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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);
4.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 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");
5.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// 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");

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.