Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Backbone 0.1.1

  • Loading branch information...
commit 184687b28fb9c5a11333b32da3b485f926da6636 1 parent 8878c22
Jeremy Ashkenas jashkenas authored
27 backbone-min.js
... ... @@ -1,14 +1,15 @@
1   -(function(){var f={};f.VERSION="0.1.0";(typeof exports!=="undefined"?exports:this).Backbone=f;var e=this._;if(!e&&typeof require!=="undefined")e=require("underscore")._;var i=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]||
  1 +(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]||
2 2 (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=
3   -{};this.cid=e.uniqueId("c");this.set(a||{},{silent:true});this._previousAttributes=e.clone(this.attributes)};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];
4   -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;
5   -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=
6   -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=
7   -function(a,b){b||(b={});if(b.comparator){this.comparator=b.comparator;delete b.comparator}this._boundOnModelEvent=e.bind(this._onModelEvent,this);this._initialize();a&&this.refresh(a,true)};e.extend(f.Collection.prototype,f.Events,{model:f.Model,add:function(a,b){if(!e.isArray(a))return this._add(a,b);for(var c=0;c<a.length;c++)this._add(a[c],b);return a},remove:function(a,b){if(!e.isArray(a))return this._remove(a,b);for(var c=0;c<a.length;c++)this._remove(a[c],b);return a},get:function(a){return a&&
8   -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){b||(b={});a=a||[];var c=this;if(a[0]&&!(a[0]instanceof f.Model))a=e.map(a,function(d){return new c.model(d)});
9   -this._initialize();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})},_initialize:function(){this.length=0;
10   -this.models=[];this._byId={};this._byCid={}},_add:function(a,b){b||(b={});var c=this.get(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);return a},_remove:function(a,b){b||(b={});a=this.get(a);if(!a)return null;delete this._byId[a.id];delete this._byCid[a.cid];
11   -delete a.collection;this.models.splice(this.indexOf(a),1);a.unbind("all",this._boundOnModelEvent);this.length--;b.silent||this.trigger("remove",a);return 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",
12   -"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._initialize(a||{});if(this.options.el)this.el=this.options.el;else{a={};if(this.id)a.id=this.id;if(this.className)a.className=this.className;this.el=this.make(this.tagName,a)}return this};var h=function(a){return $(a,this.el)},j=/^(\w+)\s*(.*)$/;
13   -e.extend(f.View.prototype,{tagName:"div",$:h,jQuery:h,render:function(){return this},make:function(a,b,c){a=document.createElement(a);b&&$(a).attr(b);c&&$(a).html(c);return a},handleEvents:function(a){$(this.el).unbind();if(!(a||(a=this.events)))return this;for(key in a){var b=a[key],c=key.match(j),d=c[1];c=c[2];b=e.bind(this[b],this);c===""||d=="change"?$(this.el).bind(d,b):$(this.el).delegate(c,d,b)}return this},_initialize:function(a){if(this.options)a=e.extend({},this.options,a);if(a.model)this.model=
14   -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 k=f.Model.extend=f.Collection.extend=f.View.extend=function(a,b){var c=i(this,a,b);c.extend=k;return c},l={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};f.sync=function(a,b,c,d){$.ajax({url:e.isFunction(b.url)?b.url():b.url,type:l[a],data:{model:JSON.stringify(b)},dataType:"json",success:c,error:d})}})();
  3 +{};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
  4 +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,
  5 +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",
  6 +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)}});
  7 +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))return this._add(a,b);for(var c=0;c<a.length;c++)this._add(a[c],b);return a},remove:function(a,b){if(!e.isArray(a))return this._remove(a,b);for(var c=0;c<a.length;c++)this._remove(a[c],
  8 +b);return a},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){b||(b={});a=a||[];var c=this;if(a[0]&&!(a[0]instanceof
  9 +f.Model))a=e.map(a,function(d){return new c.model(d)});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})},
  10 +_reset:function(){this.length=0;this.models=[];this._byId={};this._byCid={}},_add:function(a,b){b||(b={});var c=this.get(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);return a},_remove:function(a,b){b||(b={});a=this.get(a);if(!a)return null;delete this._byId[a.id];
  11 +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);return 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",
  12 +"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=
  13 +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=
  14 +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",
  15 +success:c,error:d})}})();
2  backbone.js
@@ -12,7 +12,7 @@
12 12 var Backbone = {};
13 13
14 14 // Keep the version here in sync with `package.json`.
15   - Backbone.VERSION = '0.1.0';
  15 + Backbone.VERSION = '0.1.1';
16 16
17 17 // Export for both CommonJS and the browser.
18 18 (typeof exports !== 'undefined' ? exports : this).Backbone = Backbone;
114 docs/backbone.html
57 additions, 57 deletions not shown
12 index.html
@@ -250,11 +250,11 @@ <h2 id="downloads">
250 250
251 251 <table>
252 252 <tr>
253   - <td><a href="backbone.js">Development Version (0.1.0)</a></td>
  253 + <td><a href="backbone.js">Development Version (0.1.1)</a></td>
254 254 <td><i>21kb, Uncompressed with Comments</i></td>
255 255 </tr>
256 256 <tr>
257   - <td><a href="backbone-min.js">Production Version (0.1.0)</a></td>
  257 + <td><a href="backbone-min.js">Production Version (0.1.1)</a></td>
258 258 <td><i>2kb, Packed and Gzipped</i></td>
259 259 </tr>
260 260 </table>
@@ -1298,9 +1298,15 @@ <h2 id="View">Backbone.View</h2>
1298 1298 </pre>
1299 1299
1300 1300 <h2 id="changelog">Change Log</h2>
  1301 +
  1302 + <p>
  1303 + <b class="header">0.1.1</b> &mdash; <small><i>Oct 14, 2010</i></small><br />
  1304 + Added a convention for <tt>initialize</tt> functions to be called
  1305 + upon instance construction, if defined. Documentation tweaks.
  1306 + </p>
1301 1307
1302 1308 <p>
1303   - <b class="header">0.1.0</b><br />
  1309 + <b class="header">0.1.0</b> &mdash; <small><i>Oct 13, 2010</i></small><br />
1304 1310 Initial Backbone release.
1305 1311 </p>
1306 1312

0 comments on commit 184687b

Please sign in to comment.
Something went wrong with that request. Please try again.