/vendor/backbone-articulation-latest.js

https://github.com/kmalakoff/examples-kmalakoff · JavaScript · 278 lines · 224 code · 47 blank · 7 comment · 50 complexity · 89429f62a49a73bf9dbc296dd60e9a54 MD5 · raw file

  1. // Generated by CoffeeScript 1.3.3
  2. /*
  3. backbone-articulation.js 0.3.3
  4. (c) 2011, 2012 Kevin Malakoff.
  5. Backbone-Articulation may be freely distributed under the MIT license.
  6. https://github.com/kmalakoff/backbone-articulation
  7. */
  8. (function() {
  9. var Articulation, Backbone, JSONS, LC, _,
  10. __hasProp = {}.hasOwnProperty,
  11. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  12. if (typeof require !== 'undefined') {
  13. try {
  14. _ = require('lodash');
  15. } catch (e) {
  16. _ = require('underscore');
  17. }
  18. } else {
  19. _ = this._;
  20. }
  21. if (_ && (_.hasOwnProperty('_'))) {
  22. _ = _._;
  23. }
  24. Backbone = typeof require !== 'undefined' ? require('backbone') : this.Backbone;
  25. JSONS = typeof require !== 'undefined' ? require('json-serialize') : this.JSONS;
  26. LC = typeof require !== 'undefined' ? require('lifecycle') : this.LC;
  27. Backbone.Articulation = Articulation = typeof exports !== 'undefined' ? exports : {};
  28. Articulation.VERSION = '0.3.3';
  29. Articulation.TYPE_UNDERSCORE_SINGULARIZE = false;
  30. Articulation._mixin = function(target_constructor, source_constructor, source_fns) {
  31. var Link, fn, fns, name;
  32. fns = _.pick(source_constructor.prototype, source_fns);
  33. Link = (function(_super) {
  34. __extends(Link, _super);
  35. function Link() {
  36. return Link.__super__.constructor.apply(this, arguments);
  37. }
  38. return Link;
  39. })(target_constructor.__super__.constructor);
  40. for (name in fns) {
  41. fn = fns[name];
  42. Link.prototype[name] = fn;
  43. }
  44. Link.prototype.__bba_super = target_constructor.__super__.constructor;
  45. Link.prototype.__bba_toJSON = Link.prototype['toJSON'];
  46. target_constructor.prototype.__proto__ = Link.prototype;
  47. return target_constructor.__super__ = Link.prototype;
  48. };
  49. Articulation.Model = (function(_super) {
  50. __extends(Model, _super);
  51. function Model() {
  52. return Model.__super__.constructor.apply(this, arguments);
  53. }
  54. Model.extend = Backbone.Model.extend;
  55. Model.prototype.__bba_super = Backbone.Model;
  56. Model.prototype.toJSON = function() {
  57. var class_name, json;
  58. json = JSONS.serialize(this.attributes, {
  59. properties: true
  60. });
  61. if (json.hasOwnProperty(JSONS.TYPE_FIELD)) {
  62. return json;
  63. }
  64. if (this.hasOwnProperty(JSONS.TYPE_FIELD)) {
  65. json[JSONS.TYPE_FIELD] = this[JSONS.TYPE_FIELD];
  66. return json;
  67. }
  68. class_name = Object.getPrototypeOf(Object(this)).constructor.name;
  69. if (!class_name) {
  70. return json;
  71. }
  72. if (Articulation.TYPE_UNDERSCORE_SINGULARIZE) {
  73. if (!String.prototype.underscore) {
  74. throw 'Missing String.prototype.underscore';
  75. }
  76. if (!String.prototype.singularize) {
  77. throw 'Missing String.prototype.singularize';
  78. }
  79. json[JSONS.TYPE_FIELD] = class_name.underscore().singularize();
  80. } else {
  81. json[JSONS.TYPE_FIELD] = class_name;
  82. }
  83. return json;
  84. };
  85. Model.prototype.parse = function(resp, xhr) {
  86. if (!resp) {
  87. return resp;
  88. }
  89. return JSONS.deserialize(resp, {
  90. properties: true,
  91. skip_type_field: true
  92. });
  93. };
  94. Model.prototype.set = function(attrs, options) {
  95. var key, value;
  96. if (!attrs) {
  97. return this;
  98. }
  99. if (attrs.attributes) {
  100. attrs = attrs.attributes;
  101. }
  102. for (key in attrs) {
  103. value = attrs[key];
  104. if (_.isEqual(this.attributes[key], value)) {
  105. continue;
  106. }
  107. if (this._previousAttributes && (this._previousAttributes.hasOwnProperty(key))) {
  108. this._disownAttribute(key, this._previousAttributes[key]);
  109. }
  110. this._ownAttribute(key, value);
  111. }
  112. return this.__bba_super.prototype.set.apply(this, arguments);
  113. };
  114. Model.prototype.unset = function(attr, options) {
  115. if (!(attr in this.attributes)) {
  116. return this;
  117. }
  118. this._disownAttribute(attr, this.attributes[attr]);
  119. return this.__bba_super.prototype.unset.apply(this, arguments);
  120. };
  121. Model.prototype.clear = function(options) {
  122. var key;
  123. for (key in this.attributes) {
  124. this._disownAttribute(key, this.attributes[key]);
  125. }
  126. if (options && options.silent) {
  127. for (key in this._previousAttributes) {
  128. this._disownAttribute(key, this._previousAttributes[key]);
  129. }
  130. }
  131. return this.__bba_super.prototype.clear.apply(this, arguments);
  132. };
  133. Model.prototype.change = function(options) {
  134. var key, result;
  135. for (key in this._previousAttributes) {
  136. this._disownAttribute(key, this._previousAttributes[key]);
  137. }
  138. result = this.__bba_super.prototype.change.apply(this, arguments);
  139. for (key in this._previousAttributes) {
  140. this._previousAttributes[key] = this._ownAttribute(key, this._previousAttributes[key]);
  141. }
  142. return result;
  143. };
  144. Model.prototype._ownAttribute = function(key, value) {
  145. if (!value) {
  146. return;
  147. }
  148. if ((value instanceof Backbone.Model) || (value instanceof Backbone.Collection)) {
  149. return value;
  150. }
  151. if (_.isArray(value) && value.length && (value[0] instanceof Backbone.Model) || (value[0] instanceof Backbone.Collection)) {
  152. return value;
  153. }
  154. return LC.own(value);
  155. };
  156. Model.prototype._disownAllAttributes = function() {};
  157. Model.prototype._disownAttribute = function(key, value) {
  158. if (!value) {
  159. return;
  160. }
  161. if ((value instanceof Backbone.Model) || (value instanceof Backbone.Collection)) {
  162. return value;
  163. }
  164. if (_.isArray(value) && value.length && (value[0] instanceof Backbone.Model) || (value[0] instanceof Backbone.Collection)) {
  165. return value;
  166. }
  167. return LC.disown(value);
  168. };
  169. Model.mixin = function(target_constructor) {
  170. return Articulation._mixin(target_constructor, Articulation.Model, ['toJSON', 'parse', 'set', 'unset', 'clear', 'change', '_ownAttribute', '_disownAttribute']);
  171. };
  172. return Model;
  173. })(Backbone.Model);
  174. Articulation.Collection = (function(_super) {
  175. __extends(Collection, _super);
  176. function Collection() {
  177. return Collection.__super__.constructor.apply(this, arguments);
  178. }
  179. Collection.extend = Backbone.Collection.extend;
  180. Collection.prototype.__bba_super = Backbone.Collection;
  181. Collection.prototype.model = Articulation.Model;
  182. Collection.prototype.toJSON = function() {
  183. var model, models_as_JSON, _i, _len, _ref;
  184. models_as_JSON = [];
  185. _ref = this.models;
  186. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  187. model = _ref[_i];
  188. models_as_JSON.push(model.toJSON());
  189. }
  190. return models_as_JSON;
  191. };
  192. Collection.prototype.parse = function(resp, xhr) {
  193. var articulated_model_attributes, model_resp, _i, _len;
  194. if (!(resp && _.isArray(resp))) {
  195. return resp;
  196. }
  197. articulated_model_attributes = [];
  198. for (_i = 0, _len = resp.length; _i < _len; _i++) {
  199. model_resp = resp[_i];
  200. articulated_model_attributes.push(JSONS.deserialize(model_resp, {
  201. skip_type_field: true
  202. }));
  203. }
  204. return articulated_model_attributes;
  205. };
  206. Collection.prototype._onModelEvent = function(event, model, collection, options) {
  207. var key;
  208. if (event === "destroy") {
  209. for (key in model._previousAttributes) {
  210. model._disownAttribute(key, model._previousAttributes[key]);
  211. }
  212. for (key in model.attributes) {
  213. model._disownAttribute(key, model.attributes[key]);
  214. }
  215. }
  216. return this.__bba_super.prototype._onModelEvent.apply(this, arguments);
  217. };
  218. Collection.prototype._removeReference = function(model) {
  219. if (model) {
  220. model.clear({
  221. silent: true
  222. });
  223. }
  224. return this.__bba_super.prototype._removeReference.apply(this, arguments);
  225. };
  226. Collection.mixin = function(target_constructor) {
  227. return Articulation._mixin(target_constructor, Articulation.Collection, ['toJSON', 'parse', '_reset', '_onModelEvent']);
  228. };
  229. return Collection;
  230. })(Backbone.Collection);
  231. }).call(this);