forked from jashkenas/backbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-min.js
15 lines (15 loc) · 6.99 KB
/
backbone-min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(function(){var f={};f.VERSION="0.1.1";(typeof exports!=="undefined"?exports:this).Backbone=f;var e=this._;if(!e&&typeof require!=="undefined")e=require("underscore")._;var h=this.$,j=function(a,b,c){var d=b.hasOwnProperty("constructor")?b.constructor:function(){return a.apply(this,arguments)},g=function(){};g.prototype=a.prototype;d.prototype=new g;e.extend(d.prototype,b);c&&e.extend(d,c);return d.prototype.constructor=d};f.Events={bind:function(a,b){this._callbacks||(this._callbacks={});(this._callbacks[a]||
(this._callbacks[a]=[])).push(b);return this},unbind:function(a,b){var c;if(a){if(c=this._callbacks)if(b){c=c[a];if(!c)return this;for(var d=0,g=c.length;d<g;d++)if(b===c[d]){c.splice(d,1);break}}else c[a]=[]}else this._callbacks={};return this},trigger:function(a){var b,c,d,g;if(!(c=this._callbacks))return this;if(b=c[a]){d=0;for(g=b.length;d<g;d++)b[d].apply(this,e.rest(arguments))}if(b=c.all){d=0;for(g=b.length;d<g;d++)b[d].apply(this,arguments)}return this}};f.Model=function(a){this.attributes=
{};this.cid=e.uniqueId("c");this.set(a||{},{silent:true});this._previousAttributes=e.clone(this.attributes);this.initialize&&this.initialize(a)};e.extend(f.Model.prototype,f.Events,{_previousAttributes:null,_changed:false,toJSON:function(){return e.clone(this.attributes)},get:function(a){return this.attributes[a]},set:function(a,b){b||(b={});if(!a)return this;a=a.attributes||a;var c=this.attributes;if(this.validate){var d=this.validate(a);if(d){this.trigger("error",this,d);return false}}if("id"in
a)this.id=a.id;for(var g in a){d=a[g];if(d==="")d=null;if(!e.isEqual(c[g],d)){c[g]=d;if(!b.silent){this._changed=true;this.trigger("change:"+g,this,d)}}}!b.silent&&this._changed&&this.change();return this},unset:function(a,b){b||(b={});var c=this.attributes[a];delete this.attributes[a];if(!b.silent){this._changed=true;this.trigger("change:"+a,this);this.change()}return c},save:function(a,b){a||(a={});b||(b={});if(!this.set(a,b))return false;var c=this,d=this.isNew()?"create":"update";f.sync(d,this,
function(g){if(!c.set(g.model))return false;b.success&&b.success(c,g)},b.error);return this},destroy:function(a){a||(a={});var b=this;f.sync("delete",this,function(c){b.collection&&b.collection.remove(b);a.success&&a.success(b,c)},a.error);return this},url:function(){var a=e.isFunction(this.collection.url)?this.collection.url():this.collection.url;if(this.isNew())return a;return a+"/"+this.id},clone:function(){return new this.constructor(this)},isNew:function(){return!this.id},change:function(){this.trigger("change",
this);this._previousAttributes=e.clone(this.attributes);this._changed=false},hasChanged:function(a){if(a)return this._previousAttributes[a]!=this.attributes[a];return this._changed},changedAttributes:function(a){var b=this._previousAttributes;a=a||this.attributes;var c=false,d;for(d in a)if(!e.isEqual(b[d],a[d])){c=c||{};c[d]=a[d]}return c},previous:function(a){if(!a||!this._previousAttributes)return null;return this._previousAttributes[a]},previousAttributes:function(){return e.clone(this._previousAttributes)}});
f.Collection=function(a,b){b||(b={});if(b.comparator){this.comparator=b.comparator;delete b.comparator}this._boundOnModelEvent=e.bind(this._onModelEvent,this);this._reset();a&&this.refresh(a,{silent:true});this.initialize&&this.initialize(a,b)};e.extend(f.Collection.prototype,f.Events,{model:f.Model,add:function(a,b){if(e.isArray(a))for(var c=0,d=a.length;c<d;c++)this._add(a[c],b);else this._add(a,b);return this},remove:function(a,b){if(e.isArray(a))for(var c=0,d=a.length;c<d;c++)this._remove(a[c],
b);else this._remove(a,b);return this},get:function(a){return a&&this._byId[a.id!=null?a.id:a]},getByCid:function(a){return a&&this._byCid[a.cid||a]},at:function(a){return this.models[a]},sort:function(a){a||(a={});if(!this.comparator)throw Error("Cannot sort a set without a comparator");this.models=this.sortBy(this.comparator);a.silent||this.trigger("refresh",this);return this},pluck:function(a){return e.map(this.models,function(b){return b.get(a)})},refresh:function(a,b){a||(a=[]);b||(b={});this._reset();
this.add(a,{silent:true});b.silent||this.trigger("refresh",this);return this},fetch:function(a){a||(a={});var b=this;f.sync("read",this,function(c){b.refresh(c.models);a.success&&a.success(b,c)},a.error);return this},create:function(a,b){b||(b={});a instanceof f.Model||(a=new this.model(a));a.collection=this;return a.save(null,{success:function(c){if(!a.set(c.model))return false;a.collection.add(a);b.success&&b.success(a,c)},error:b.error})},_reset:function(){this.length=0;this.models=[];this._byId=
{};this._byCid={}},_add:function(a,b){b||(b={});a instanceof f.Model||(a=new this.model(a));var c=this.getByCid(a);if(c)throw Error(["Can't add the same model to a set twice",c.id]);this._byId[a.id]=a;this._byCid[a.cid]=a;a.collection=this;this.models.splice(this.comparator?this.sortedIndex(a,this.comparator):this.length,0,a);a.bind("all",this._boundOnModelEvent);this.length++;b.silent||this.trigger("add",a)},_remove:function(a,b){b||(b={});a=this.getByCid(a);if(!a)return null;delete this._byId[a.id];
delete this._byCid[a.cid];delete a.collection;this.models.splice(this.indexOf(a),1);a.unbind("all",this._boundOnModelEvent);this.length--;b.silent||this.trigger("remove",a)},_onModelEvent:function(a,b,c){switch(a){case "change":if(b.hasChanged("id")){delete this._byId[b.previous("id")];this._byId[b.id]=b}this.trigger("change",b);break;case "error":this.trigger("error",b,c)}}});e.each(["forEach","each","map","reduce","reduceRight","find","detect","filter","select","reject","every","all","some","any",
"include","invoke","max","min","sortBy","sortedIndex","toArray","size","first","rest","last","without","indexOf","lastIndexOf","isEmpty"],function(a){f.Collection.prototype[a]=function(){return e[a].apply(e,[this.models].concat(e.toArray(arguments)))}});f.View=function(a){this._configure(a||{});if(this.options.el)this.el=this.options.el;else{var b={};if(this.id)b.id=this.id;if(this.className)b.className=this.className;this.el=this.make(this.tagName,b)}this.initialize&&this.initialize(a)};var i=function(a){return h(a,
this.el)},k=/^(\w+)\s*(.*)$/;e.extend(f.View.prototype,{tagName:"div",$:i,jQuery:i,render:function(){return this},make:function(a,b,c){a=document.createElement(a);b&&h(a).attr(b);c&&h(a).html(c);return a},handleEvents:function(a){h(this.el).unbind();if(!(a||(a=this.events)))return this;for(key in a){var b=a[key],c=key.match(k),d=c[1];c=c[2];b=e.bind(this[b],this);c===""||d=="change"?h(this.el).bind(d,b):h(this.el).delegate(c,d,b)}return this},_configure:function(a){if(this.options)a=e.extend({},this.options,
a);if(a.model)this.model=a.model;if(a.collection)this.collection=a.collection;if(a.id)this.id=a.id;if(a.className)this.className=a.className;if(a.tagName)this.tagName=a.tagName;this.options=a}});var l=f.Model.extend=f.Collection.extend=f.View.extend=function(a,b){var c=j(this,a,b);c.extend=l;return c},m={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};f.sync=function(a,b,c,d){h.ajax({url:e.isFunction(b.url)?b.url():b.url,type:m[a],data:{model:JSON.stringify(b)},dataType:"json",success:c,
error:d})}})();