forked from pathable/supermodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupermodel.min.js
More file actions
1 lines (1 loc) · 6.22 KB
/
supermodel.min.js
File metadata and controls
1 lines (1 loc) · 6.22 KB
1
(function(){var e;typeof exports=="undefined"?e=this.Supermodel={}:e=exports,e.VERSION="0.0.4";var t=this._;!t&&typeof require!="undefined"&&(t=require("underscore"));var n=this.Backbone;!n&&typeof require!="undefined"&&(n=require("backbone"));var r=function(e,n){this.required(n,"name"),t.extend(this,t.pick(n,"name","where","source","store")),t.defaults(this,{source:this.name,store:"_"+this.name});var r=e;do{if(!r.associations()[this.name])continue;throw new Error("Association already exists: "+this.name)}while(r=r.parent);e.associations()[this.name]=this,this.initialize&&e.all().on("initialize",this.initialize,this),this.change&&e.all().on("change",this.change,this),this.parse&&e.all().on("parse",this.parse,this),this.destroy&&e.all().on("destroy",this.destroy,this),this.create&&e.all().on("add",this.create,this)};t.extend(r.prototype,{associate:function(e,t){if(!this.inverse)return;e.trigger("associate:"+this.inverse,e,t)},dissociate:function(e,t){if(!this.inverse)return;e.trigger("dissociate:"+this.inverse,e,t)},required:function(e){var t;for(var n=1;n<arguments.length;n++){if(e[t=arguments[n]])continue;throw new Error("Option required: "+t)}},andThis:function(e){var n=this;return function(){return e.apply(n,[this].concat(t.toArray(arguments)))}}});var i=function(e,n){this.required(n,"inverse","model"),r.apply(this,arguments),t.extend(this,t.pick(n,"inverse","model")),t.defaults(this,{id:this.name+"_id"}),e.all().on("associate:"+this.name,this.replace,this).on("dissociate:"+this.name,this.remove,this)};t.extend(i.prototype,r.prototype,{create:function(e){e[this.name]=t.bind(this.access,this,e)},access:function(e,t){if(arguments.length<2)return e[this.store];this.replace(e,t)},initialize:function(e){this.parse(e,e.attributes);var t=e.get(this.id);t!=null&&this.replace(e,t)},parse:function(e,n){if(!n||!t.has(n,this.source))return;var r=n[this.source];delete n[this.source],this.replace(e,r)},change:function(e){if(!e.hasChanged(this.id))return;this.replace(e,e.get(this.id))},remove:function(e){this.replace(e,null)},destroy:function(e){var t=e[this.store];if(!t)return;this.remove(e),this.dissociate(t,e)},replace:function(e,n){var r,i;if(!e)return;i=e[this.store],n!=null&&!t.isObject(n)&&(r=n,(n={})[this.model.prototype.idAttribute]=r),n&&!(n instanceof a)&&(n=this.model.create(n));if(i===n)return;n||e.unset(this.id),i&&(delete e[this.store],this.dissociate(i,e));if(!n)return;e.set(this.id,n.id),e[this.store]=n,this.associate(n,e)}});var s=function(e,n){this.required(n,"inverse","collection"),r.apply(this,arguments),t.extend(this,t.pick(n,"collection","inverse")),e.all().on("associate:"+this.name,this._associate,this).on("dissociate:"+this.name,this._dissociate,this)};t.extend(s.prototype,r.prototype,{create:function(e){e[this.name]||(e[this.name]=t.bind(this.get,this,e))},get:function(e){var t=e[this.store];return t?t:(t=e[this.store]=(new this.collection).on("add",this.add,this).on("remove",this.remove,this).on("reset",this.reset,this),t.owner=e,t)},parse:function(e,n){if(!n)return;var r=n[this.source];if(!r)return;delete n[this.source];var i=this.get(e);r=i.parse(r);if(!this.where){i.reset(r);return}i.reset(t.filter(t.map(r,function(e){return new i.model(e)}),this.where))},initialize:function(e){this.parse(e,e.attributes)},add:function(e,t){if(!e||!t)return;this.associate(e,t.owner)},remove:function(e,t){if(!e||!t)return;this.dissociate(e,t.owner)},reset:function(e){if(!e)return;e.each(function(t){this.associate(t,e.owner)},this)},destroy:function(e){var t;if(!e||!(t=e[this.store]))return;t.each(function(t){this.dissociate(t,e)},this)},_associate:function(e,t){if(!e||!t)return;if(this.where&&!this.where(t))return;this.get(e).add(t)},_dissociate:function(e,t){if(!e||!t||!e[this.store])return;e[this.store].remove(t)}});var o=function(e,n){this.required(n,"collection","through","source"),r.apply(this,arguments),t.extend(this,t.pick(n,"collection","through")),this._associate=this.andThis(this._associate),this._dissociate=this.andThis(this._dissociate)};t.extend(o.prototype,r.prototype,{create:function(e){e[this.name]||(e[this.name]=t.bind(this.get,this,e))},get:function(e){var t=e[this.store];return t?t:(t=new this.collection,t.owner=e,e[this.store]=t,this.reset(e[this.through]().on("add",this.add,this).on("remove",this.remove,this).on("reset",this.reset,this).on("associate:"+this.source,this._associate).on("dissociate:"+this.source,this._dissociate)),t)},add:function(e,t){if(!e||!t||!(e=e[this.source]()))return;if(this.where&&!this.where(e))return;t.owner[this.name]().add(e)},remove:function(e,t){if(!e||!t||!(e=e[this.source]()))return;var n=t.any(function(t){return t[this.source]()===e},this);n||t.owner[this.name]().remove(e)},reset:function(e){if(!e)return;var n=t.compact(t.uniq(t.invoke(e.models,this.source)));this.where&&(n=t.filter(n,this.where)),e.owner[this.name]().reset(n)},_associate:function(e,t,n){if(!e||!t||!n)return;if(this.where&&!this.where(n))return;e.owner[this.name]().add(n)},_dissociate:function(e,t,n){if(!e||!t||!n)return;var r=e.any(function(e){return e[this.source]()===n},this);r||e.owner[this.name]().remove(n)}});var u=function(e){this.model=e};t.extend(u.prototype,{one:function(e,t){return t.name=e,new i(this.model,t),this},many:function(e,t){t.name=e;var n=t.through?o:s;return new n(this.model,t),this}});var a=e.Model=n.Model.extend({cidAttribute:"cid",initialize:function(){this.set(this.cidAttribute,this.cid);var e=this.constructor;do{var t=this.collection;e.all().add(this),this.collection=t}while(e=e.parent);this.trigger("initialize",this)},toJSON:function(){var e=n.Model.prototype.toJSON.apply(this,arguments);return delete e[this.cidAttribute],e},parse:function(e){return this.trigger("parse",this,e),e}},{create:function(e,t){var n,r=this.all(),i=e&&e[this.prototype.cidAttribute],s=e&&e[this.prototype.idAttribute];if(i&&(n=r.getByCid(i))&&n.attributes===e)return n;if(s&&(n=r.get(s)))return n.parse(e),n.set(e),n;if(!s)return new this(e,t);var o=this;do{if(!o.all().get(s))continue;throw new Error('Model with id "'+s+'" already exists.')}while(o=o.parent);return new this(e,t)},has:function(){return new u(this)},all:function(){return this._all||(this._all=new n.Collection)},associations:function(){return this._associations||(this._associations={})},reset:function(){this._all=new n.Collection,this._associations={}}})}).call(this);