Last active
December 24, 2015 18:19
-
-
Save gurdotan/6842680 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
var BackboneViewExtensions = { | |
mixin : function(from) { | |
var to = this.prototype; | |
// we add those methods which exists on `from` but not on `to` to the latter | |
_.defaults(to, from); | |
// ...and we do the same for events and triggers | |
_.defaults(to.events, from.events); | |
_.defaults(to.triggers, from.triggers); | |
// we then extend `to`'s `initialize` | |
BackboneExtensions.extendMethod(to, from, "initialize"); | |
// … and its `render` | |
Utils.extendMethod(to, from, "render"); | |
}, | |
// Helper method to extend an already existing method | |
extendMethod : function(to, from, methodName) { | |
// if the method is defined on from ... | |
if (!_.isUndefined(from[methodName])) { | |
var old = to[methodName]; | |
// ... we create a new function on to | |
to[methodName] = function() { | |
// wherein we first call the method which exists on `to` | |
var oldReturn = old.apply(this, arguments); | |
// and then call the method on `from` | |
from[methodName].apply(this, arguments); | |
// and then return the expected result, | |
// i.e. what the method on `to` returns | |
return oldReturn; | |
}; | |
} | |
} | |
}; | |
_.extend(Backbone.View.prototype, BackboneViewExtensions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment