-
-
Save craigmaslowski/5759511 to your computer and use it in GitHub Desktop.
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
Backbone.mixin = function (obj, mixin) { | |
var source = obj.prototype || obj; | |
if (!mixin) | |
throw new Error('Backbone.mixin: The mixin specified is undefined.'); | |
// merge all properties in the mixin into the view's prototype | |
_.defaults(source, mixin); | |
// merge object literal properties (ensure's events, bindings, and channels) | |
_.each(['events', 'bindings', 'attributes', 'tagName'], function (key) { | |
if (mixin[key]) { | |
if (source[key]) { | |
_.defaults(source[key], mixin[key]); | |
} else { | |
source[key] = mixin[key]; | |
} | |
} | |
}); | |
// setup render chain | |
if (mixin.render) { | |
var oldRender = source.render; | |
source.render = function () { | |
mixin.render.apply(this, arguments); | |
var retVal = oldRender.apply(this, arguments); | |
if (retVal) { | |
return retVal; | |
} | |
return this; | |
}; | |
} | |
// setup initialize chain | |
if (mixin.initialize) { | |
var oldInitialize = source.initialize; | |
source.initialize = function () { | |
mixin.initialize.apply(this, arguments); | |
oldInitialize.apply(this, arguments); | |
}; | |
} | |
// setup remove chain | |
if (mixin.remove) { | |
var oldRemove = source.remove; | |
source.remove = function () { | |
mixin.remove.apply(this, arguments); | |
oldRemove.apply(this, arguments); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment