PageRenderTime 69ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/backbone-super/1.0.2/backbone-super.js

https://gitlab.com/alidz1982/cdnjs
JavaScript | 107 lines | 61 code | 20 blank | 26 comment | 16 complexity | 2637e18ba73edd6a232902286afd88a4 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(root, factory) {
  5. // Set up Backbone appropriately for the environment. Start with AMD.
  6. if (typeof define === 'function' && define.amd) {
  7. define(['underscore', 'backbone'], function(_, Backbone) {
  8. // Export global even in AMD case in case this script is loaded with
  9. // others that may still expect a global Backbone.
  10. factory( _, Backbone);
  11. });
  12. // Next for Node.js or CommonJS.
  13. } else if (typeof exports !== 'undefined') {
  14. var _ = require('underscore'),
  15. Backbone = require('backbone');
  16. factory(_, Backbone);
  17. // Finally, as a browser global.
  18. } else {
  19. factory(root._, root.Backbone);
  20. }
  21. }(this, function factory(_, Backbone) {
  22. Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = function(protoProps, classProps) {
  23. var child = inherits(this, protoProps, classProps);
  24. child.extend = this.extend;
  25. return child;
  26. };
  27. var unImplementedSuper = function(method){throw "Super does not implement this method: " + method;};
  28. var ctor = function(){}, inherits = function(parent, protoProps, staticProps) {
  29. var child, _super = parent.prototype, fnTest = /\b_super\b/;
  30. // The constructor function for the new subclass is either defined by you
  31. // (the "constructor" property in your `extend` definition), or defaulted
  32. // by us to simply call the parent's constructor.
  33. if (protoProps && protoProps.hasOwnProperty('constructor')) {
  34. child = protoProps.constructor;
  35. } else {
  36. child = function(){ return parent.apply(this, arguments); };
  37. }
  38. // Inherit class (static) properties from parent.
  39. _.extend(child, parent, staticProps);
  40. // Set the prototype chain to inherit from `parent`, without calling
  41. // `parent`'s constructor function.
  42. ctor.prototype = parent.prototype;
  43. child.prototype = new ctor();
  44. // Add prototype properties (instance properties) to the subclass,
  45. // if supplied.
  46. if (protoProps) {
  47. _.extend(child.prototype, protoProps);
  48. // Copy the properties over onto the new prototype
  49. for (var name in protoProps) {
  50. // Check if we're overwriting an existing function
  51. if (typeof protoProps[name] == "function" && fnTest.test(protoProps[name])) {
  52. child.prototype[name] = (function(name, fn) {
  53. var wrapper = function() {
  54. var tmp = this._super;
  55. // Add a new ._super() method that is the same method
  56. // but on the super-class
  57. this._super = _super[name] || unImplementedSuper(name);
  58. // The method only need to be bound temporarily, so we
  59. // remove it when we're done executing
  60. var ret;
  61. try {
  62. ret = fn.apply(this, arguments);
  63. } finally {
  64. this._super = tmp;
  65. }
  66. return ret;
  67. };
  68. //we must move properties from old function to new
  69. for (var prop in fn) {
  70. wrapper[prop] = fn[prop];
  71. delete fn[prop];
  72. }
  73. return wrapper;
  74. })(name, protoProps[name]);
  75. }
  76. }
  77. }
  78. // Add static properties to the constructor function, if supplied.
  79. if (staticProps) _.extend(child, staticProps);
  80. // Correctly set child's `prototype.constructor`.
  81. child.prototype.constructor = child;
  82. // Set a convenience property in case the parent's prototype is needed later.
  83. child.__super__ = parent.prototype;
  84. return child;
  85. };
  86. return inherits;
  87. }));