/* backbone-articulation.js 0.3.4 (c) 2011, 2012 Kevin Malakoff - http://kmalakoff.github.com/backbone-articulation/ License: MIT (http://www.opensource.org/licenses/mit-license.php) Dependencies: Backbone.js, and Underscore.js. */(function(){return function(e){return typeof define=="function"&&define.amd?define("backbone-articulation",["underscore","backbone","json-serialize","lifecycle"],e):e.call(this)}(function(){var e,t,n,r,i,s={}.hasOwnProperty,o=function(e,t){function r(){this.constructor=e}for(var n in t)s.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};if(!this._&&typeof require!="undefined")try{i=require("lodash")}catch(u){i=require("underscore")}else i=this._;return i=i.hasOwnProperty("_")?i._:i,t=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,n=!this.JSONS&&typeof require!="undefined"?require("json-serialize"):this.JSONS,r=!this.LC&&typeof require!="undefined"?require("lifecycle"):this.LC,t.Articulation=e=typeof exports!="undefined"?exports:{},e.VERSION="0.3.4",e.TYPE_UNDERSCORE_SINGULARIZE=!1,e._mixin=function(e,t,n){var r,s,u,a;u=i.pick(t.prototype,n),r=function(e){function t(){return t.__super__.constructor.apply(this,arguments)}return o(t,e),t}(e.__super__.constructor);for(a in u)s=u[a],r.prototype[a]=s;return r.prototype.__bba_super=e.__super__.constructor,r.prototype.__bba_toJSON=r.prototype.toJSON,e.prototype.__proto__=r.prototype,e.__super__=r.prototype},e.Model=function(s){function u(){return u.__super__.constructor.apply(this,arguments)}return o(u,s),u.extend=t.Model.extend,u.prototype.__bba_super=t.Model,u.prototype.toJSON=function(){var t,r;r=n.serialize(this.attributes,{properties:!0});if(r.hasOwnProperty(n.TYPE_FIELD))return r;if(this[n.TYPE_FIELD])return r[n.TYPE_FIELD]=this[n.TYPE_FIELD],r;t=Object.getPrototypeOf(Object(this)).constructor.name;if(!t)return r;if(e.TYPE_UNDERSCORE_SINGULARIZE){if(!String.prototype.underscore)throw"Missing String.prototype.underscore";if(!String.prototype.singularize)throw"Missing String.prototype.singularize";r[n.TYPE_FIELD]=t.underscore().singularize()}else r[n.TYPE_FIELD]=t;return r},u.prototype.parse=function(e,t){return e?n.deserialize(e,{properties:!0,skip_type_field:!0}):e},u.prototype.set=function(e,t){var n,r;if(!e)return this;e.attributes&&(e=e.attributes);for(n in e){r=e[n];if(i.isEqual(this.attributes[n],r))continue;this._previousAttributes&&this._previousAttributes.hasOwnProperty(n)&&this._disownAttribute(n,this._previousAttributes[n]),this._ownAttribute(n,r)}return this.__bba_super.prototype.set.apply(this,arguments)},u.prototype.unset=function(e,t){return e in this.attributes?(this._disownAttribute(e,this.attributes[e]),this.__bba_super.prototype.unset.apply(this,arguments)):this},u.prototype.clear=function(e){var t;for(t in this.attributes)this._disownAttribute(t,this.attributes[t]);if(e&&e.silent)for(t in this._previousAttributes)this._disownAttribute(t,this._previousAttributes[t]);return this.__bba_super.prototype.clear.apply(this,arguments)},u.prototype.change=function(e){var t,n;for(t in this._previousAttributes)this._disownAttribute(t,this._previousAttributes[t]);n=this.__bba_super.prototype.change.apply(this,arguments);for(t in this._previousAttributes)this._previousAttributes[t]=this._ownAttribute(t,this._previousAttributes[t]);return n},u.prototype._ownAttribute=function(e,n){if(!n)return;return n instanceof t.Model||n instanceof t.Collection?n:i.isArray(n)&&n.length&&n[0]instanceof t.Model||n[0]instanceof t.Collection?n:r.own(n)},u.prototype._disownAllAttributes=function(){},u.prototype._disownAttribute=function(e,n){if(!n)return;return n instanceof t.Model||n instanceof t.Collection?n:i.isArray(n)&&n.length&&n[0]instanceof t.Model||n[0]instanceof t.Collection?n:r.disown(n)},u.mixin=function(t){return e._mixin(t,e.Model,["toJSON","parse","set","unset","clear","change","_ownAttribute","_disownAttribute"])},u}(t.Model),e.Collection=function(r){function s(){return s.__super__.constructor.apply(this,arguments)}return o(s,r),s.extend=t.Collection.extend,s.prototype.__bba_super=t.Collection,s.prototype.model=e.Model,s.prototype.toJSON=function(){var e,t,n,r,i;t=[],i=this.models;for(n=0,r=i.length;n