Last active
August 29, 2015 14:04
-
-
Save sarink/a3cf3f08c17691395edf 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
// Forked/modified from: https://gist.github.com/maxbrunsfeld/1542120 | |
// This method gives you an easier way of calling super when you're using Backbone in plain javascript. | |
// It lets you avoid writing the constructor's name multiple times. | |
// You still have to specify the name of the method. | |
// | |
// So, instead of having to write: | |
// | |
// var Animal = Backbone.Model.extend({ | |
// word: "", | |
// say: function() { | |
// return "I say " + this.word; | |
// } | |
// }); | |
// var Cow = Animal.extend({ | |
// word: "moo", | |
// say: function() { | |
// return Animal.prototype.say.apply(this, arguments) + "!!!" | |
// } | |
// }); | |
// | |
// | |
// You get to write: | |
// | |
// var Animal = Backbone.Model.extend({ | |
// word: "", | |
// say: function() { | |
// return "I say " + this.word; | |
// } | |
// }); | |
// var Cow = Animal.extend({ | |
// word: "moo", | |
// say: function() { | |
// return this._super("say", arguments) + "!!!" | |
// } | |
// }); | |
(function(root, factory) { | |
if (typeof define === "function" && define.amd) { | |
define(["underscore", "backbone"], function(_, Backbone) { | |
return factory(Backbone); | |
}); | |
} | |
else if (typeof exports !== "undefined") { | |
var _ = require("underscore"); | |
var Backbone = require("backbone"); | |
module.exports = factory(_, Backbone); | |
} | |
else { | |
factory(root._, root.Backbone); | |
} | |
}(this, function(_, Backbone) { | |
"use strict"; | |
// Finds the next object up the prototype chain that has a different implementation of the method. | |
var findSuper = function(methodName, childObject) { | |
var object = childObject; | |
while (object[methodName] === childObject[methodName]) { | |
object = object.constructor.__super__; | |
} | |
return object; | |
}; | |
var _super = function(methodName) { | |
// Keep track of how far up the prototype chain we have traversed, in order to handle nested calls to `_super`. | |
this.__superCallObjects__ || (this.__superCallObjects__ = {}); | |
var currentObject = this.__superCallObjects__[methodName] || this; | |
var parentObject = findSuper(methodName, currentObject); | |
this.__superCallObjects__[methodName] = parentObject; | |
// If `methodName` is a function, call it with `this` as the context and `args` as the arguments, if it's an object, simply return it. | |
var args = _.tail(arguments); | |
var result = (_.isFunction(parentObject[methodName])) ? parentObject[methodName].apply(this, args) : parentObject[methodName]; | |
delete this.__superCallObjects__[methodName]; | |
return result; | |
}; | |
// Mix in to Backbone classes | |
_.each(["Model", "Collection", "View", "Router"], function(klass) { | |
Backbone[klass].prototype._super = _super; | |
}); | |
return Backbone; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment