forked from benpickles/js-model
/
js-model-0.8.4.min.js
16 lines (16 loc) · 5.09 KB
/
js-model-0.8.4.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* js-model JavaScript library, version 0.8.4
* (c) 2010 Ben Pickles
*
* Released under MIT license.
*/
var Model=function(a,b,c){b=b||{};c=c||{};var g=function(d){this.attributes=d||{};this.changes={};this.errors=new Model.Errors(this)};jQuery.extend(g,Model.Callbacks,Model.ClassMethods,b,{_name:a,collection:[],chain:function(d){return jQuery.extend({},this,{collection:d})}});jQuery.extend(g.prototype,Model.Callbacks,Model.InstanceMethods,c);return g};
Model.Callbacks={callbacks:{},bind:function(a,b){this.callbacks[a]=this.callbacks[a]||[];this.callbacks[a].push(b);return this},trigger:function(a,b){if(a=this.callbacks[a])for(var c=0;c<a.length;c++)a[c].apply(this,b||[]);return this},unbind:function(a,b){if(b)for(var c=this.callbacks[a]||[],g=0;g<c.length;g++)c[g]===b&&this.callbacks[a].splice(g,1);else delete this.callbacks[a];return this}};
Model.ClassMethods={add:function(){for(var a=[],b=0;b<arguments.length;b++){var c=arguments[b];if(!this.detect(function(){return this.id()!==null&&this.id()==c.id()})){this.collection.push(c);a.push(c)}}a.length>0&&this.trigger("add",a);return this},all:function(){return this.collection},count:function(){return this.collection.length},detect:function(a){var b;jQuery.each(this.all(),function(c){if(a.call(this,c)){b=this;return false}});return b||null},each:function(a){jQuery.each(this.all(),function(b){a.call(this,
b)});return this},find:function(a){return this.detect(function(){return this.id()==a})||null},first:function(){return this.all()[0]||null},last:function(){var a=this.all();return a[a.length-1]||null},remove:function(a){var b=_.invoke(this.collection,"id");a=_.indexOf(b,a);if(a>-1){this.collection.splice(a,1);this.trigger("remove");return true}else return false},select:function(a){var b=[];jQuery.each(this.all(),function(c){a.call(this,c)&&b.push(this)});return this.chain(b)},sort:function(a){return this.chain(_.sortBy(this.all(),
function(b,c){return a.call(b,c)}))}};Model.Errors=function(a){this.errors={};this.model=a};Model.Errors.prototype={add:function(a,b){this.errors[a]||(this.errors[a]=[]);this.errors[a].push(b)},all:function(){return this.errors},clear:function(){this.errors={}},each:function(a){for(var b in this.errors)for(var c=0;c<this.errors[b].length;c++)a.call(this,b,this.errors[b][c])},on:function(a){return this.errors[a]||[]},size:function(){var a=0;this.each(function(){a++});return a}};
Model.InstanceMethods={attr:function(a,b){if(arguments.length===0)return jQuery.extend({},this.attributes,this.changes);else if(arguments.length===2){if(_.isEqual(this.attributes[a],b))delete this.changes[a];else this.changes[a]=b;return this}else if(typeof a==="object"){for(var c in a)this.attr(c,a[c]);return this}else return a in this.changes?this.changes[a]:this.attributes[a]},callPersistMethod:function(a,b){var c=this,g=function(){if(a==="create")c.constructor.add(c);else a==="destroy"&&c.constructor.remove(c.id())},
d=function(e){if(e){c.merge(c.changes).reset();g();c.trigger(a)}var f;if(b)f=b.apply(c,arguments);return f};this.constructor.persistence?this.constructor.persistence[a](this,d):d.call(this,true)},destroy:function(a){this.callPersistMethod("destroy",a);return this},id:function(){return this.attributes.id||null},merge:function(a){jQuery.extend(this.attributes,a);return this},newRecord:function(){return this.id()===null},reset:function(){this.errors.clear();this.changes={};return this},save:function(a){if(this.valid())this.callPersistMethod(this.newRecord()?
"create":"update",a);else a&&a(false);return this},update:function(a){this.merge(a).trigger("update");return this},valid:function(){this.errors.clear();this.validate();return this.errors.size()===0},validate:function(){return this}};Model.Log=function(){window.console&&window.console.log.apply(window.console,arguments)};
Model.RestPersistence=function(a,b){var c=/:([\w\d]+)/g,g=function(){this.resource=a;for(this.resource_param_names=[];(param_name=c.exec(a))!==null;)this.resource_param_names.push(param_name[1])};jQuery.extend(g.prototype,{path:function(d){var e=this.resource;$.each(this.resource_param_names,function(f,h){e=e.replace(":"+h,d.attributes[h])});return e},create:function(d,e){return this.xhr("POST",this.create_path(d),d,e)},create_path:function(d){return this.path(d)},destroy:function(d,e){return this.xhr("DELETE",
this.destroy_path(d),d,e)},destroy_path:function(d){return this.update_path(d)},params:function(d){var e;if(d){var f=d.attr();delete f.id;e={};e[d.constructor._name.toLowerCase()]=f}else e=null;return e},parseResponseData:function(d){try{return/\S/.test(d.responseText)?jQuery.parseJSON(d.responseText):null}catch(e){Model.Log(e)}},update:function(d,e){return this.xhr("PUT",this.update_path(d),d,e)},update_path:function(d){return[this.path(d),d.id()].join("/")},xhr:function(d,e,f,h){var i=this,j=d===
"DELETE"?null:this.params(f);return jQuery.ajax({type:d,url:e,dataType:"json",data:j,complete:function(k,l){i.xhrComplete(k,l,f,h)}})},xhrComplete:function(d,e,f,h){var i=this.parseResponseData(d);e=e==="success";if(i)if(e)f.attr(i);else if(d.status===422){f.errors.clear();for(var j in i)for(var k=0;k<i[j].length;k++)f.errors.add(j,i[j][k])}h&&h.call(f,e,d)}},b);return new g};