PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/npm/backbone-articulation.js

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