-
-
Save KennyLisc/3856840 to your computer and use it in GitHub Desktop.
javascript mixins
This file contains 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
var foo = { | |
doSomething: function(){ | |
// ... | |
} | |
} | |
var bar = {}; | |
bar.doSomething = foo.doSomething; |
This file contains 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
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 |
This file contains 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
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); |
This file contains 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
// 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"); |
This file contains 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
// 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