PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/vendor/scripts/backbone.super.js

https://github.com/zakeddington/game-air-hockey
JavaScript | 83 lines | 48 code | 14 blank | 21 comment | 11 complexity | 613526d637c124ce8b1153ab7d1343b8 MD5 | raw file
  1. // This is a plugin, constructed from parts of Backbone.js and John Resig's inheritance script.
  2. // (See http://backbonejs.org, http://ejohn.org/blog/simple-javascript-inheritance/)
  3. // No credit goes to me as I did absolutely nothing except patch these two together.
  4. (function(Backbone) {
  5. Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = function(protoProps, classProps) {
  6. var child = inherits(this, protoProps, classProps);
  7. child.extend = this.extend;
  8. return child;
  9. };
  10. var unImplementedSuper = function(method){throw "Super does not implement this method: " + method;};
  11. var ctor = function(){}, inherits = function(parent, protoProps, staticProps) {
  12. var child, _super = parent.prototype, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  13. // The constructor function for the new subclass is either defined by you
  14. // (the "constructor" property in your `extend` definition), or defaulted
  15. // by us to simply call the parent's constructor.
  16. if (protoProps && protoProps.hasOwnProperty('constructor')) {
  17. child = protoProps.constructor;
  18. } else {
  19. child = function(){ return parent.apply(this, arguments); };
  20. }
  21. // Inherit class (static) properties from parent.
  22. _.extend(child, parent, staticProps);
  23. // Set the prototype chain to inherit from `parent`, without calling
  24. // `parent`'s constructor function.
  25. ctor.prototype = parent.prototype;
  26. child.prototype = new ctor();
  27. // Add prototype properties (instance properties) to the subclass,
  28. // if supplied.
  29. if (protoProps) {
  30. _.extend(child.prototype, protoProps);
  31. // Copy the properties over onto the new prototype
  32. for (var name in protoProps) {
  33. // Check if we're overwriting an existing function
  34. if (typeof protoProps[name] == "function" && fnTest.test(protoProps[name])) {
  35. child.prototype[name] = (function(name, fn) {
  36. var wrapper = function() {
  37. var tmp = this._super;
  38. // Add a new ._super() method that is the same method
  39. // but on the super-class
  40. this._super = _super[name] || unImplementedSuper(name);
  41. // The method only need to be bound temporarily, so we
  42. // remove it when we're done executing
  43. var ret;
  44. try {
  45. ret = fn.apply(this, arguments);
  46. } finally {
  47. this._super = tmp;
  48. }
  49. return ret;
  50. };
  51. //we must move properties from old function to new
  52. for (var prop in fn) {
  53. wrapper[prop] = fn[prop];
  54. delete fn[prop];
  55. }
  56. return wrapper;
  57. })(name, protoProps[name]);
  58. }
  59. }
  60. }
  61. // Add static properties to the constructor function, if supplied.
  62. if (staticProps) _.extend(child, staticProps);
  63. // Correctly set child's `prototype.constructor`.
  64. child.prototype.constructor = child;
  65. // Set a convenience property in case the parent's prototype is needed later.
  66. child.__super__ = parent.prototype;
  67. return child;
  68. };
  69. })(Backbone);