-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.min.js
1 lines (1 loc) · 1.26 KB
/
model.min.js
1
function Model(a,b){var c=function(c){if(b){var d=a.init?a.init:b}else{var d=a.init?a.init:Model.core.init}d.call(this,c);if(b){this.parent=new b(c)}};if(b){Model.util.apply(b,c);Model.util.apply(b.prototype,c.prototype)}Model.util.apply(a.statics,c);Model.util.apply(a.methods,c.prototype);if(a.cacheKey){c.cacheKey=a.cacheKey;c.instances={};if(a.statics&&a.statics.fetch){c._fetch=a.statics.fetch}else{throw"ModelJS Error: You must define a custom `fetch` method when defining a cacheKey"}if(!a.methods||a.methods&&!a.methods.save){c.prototype.save=Model.core.save(c)}c.cache=Model.core.cache(c);c.fetch=Model.core.fetch(c)}c.extend=Model.core.extend(c);return c}Model.util={apply:function(a,b){for(item in a){b[item]=a[item]}}};Model.core={init:function(a){this.attributes=a;for(attribute in a){this[attribute]=a[attribute]}},fetch:function(a){return function(b,c,d){if(d){a._fetch(b,c)}else{instance=a.instances[String(b)];if(c){if(instance){c(instance)}else{a._fetch(b,c)}}else{return instance||null}}}},cache:function(a){return function(b){key=b[a.cacheKey];if(key instanceof Function){key=b[a.cacheKey]()}a.instances[String(key)]=b}},save:function(a){return function(b){a.cache(this);if(b)b(this)}},extend:function(a){return function(b){return new Model(b,a)}}};Model.version="0.4.2"