Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'original' (Updating to 0.9.1)

Conflicts:
	backbone.js
	package.json
  • Loading branch information...
commit 4861fd7f6e9f24f841734eb2203f213c84317d6a 2 parents 2c6c1e0 + 65fa4f4
@claudioc authored
View
64 backbone-min.js
@@ -1,35 +1,37 @@
-// Backbone.js 0.9.0
+// Backbone.js 0.9.1
+
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
// http://backbonejs.org
-(function(){var i=this,r=i.Backbone,s=Array.prototype.slice,t=Array.prototype.splice,g;g=typeof exports!=="undefined"?exports:i.Backbone={};g.VERSION="0.9.0";var f=i._;!f&&typeof require!=="undefined"&&(f=require("underscore"));var h=i.jQuery||i.Zepto||i.ender;g.noConflict=function(){i.Backbone=r;return this};g.emulateHTTP=false;g.emulateJSON=false;g.Events={on:function(a,b,c){for(var d,a=a.split(/\s+/),e=this._callbacks||(this._callbacks={});d=a.shift();){d=e[d]||(e[d]={});var f=d.tail||(d.tail=
-d.next={});f.callback=b;f.context=c;d.tail=f.next={}}return this},off:function(a,b,c){var d,e,f;if(a){if(e=this._callbacks)for(a=a.split(/\s+/);d=a.shift();)if(f=e[d],delete e[d],b&&f)for(;(f=f.next)&&f.next;)if(!(f.callback===b&&(!c||f.context===c)))this.on(d,f.callback,f.context)}else delete this._callbacks;return this},trigger:function(a){var b,c,d,e;if(!(d=this._callbacks))return this;e=d.all;for((a=a.split(/\s+/)).push(null);b=a.shift();)e&&a.push({next:e.next,tail:e.tail,event:b}),(c=d[b])&&
-a.push({next:c.next,tail:c.tail});for(e=s.call(arguments,1);c=a.pop();){b=c.tail;for(d=c.event?[c.event].concat(e):e;(c=c.next)!==b;)c.callback.apply(c.context||this,d)}return this}};g.Events.bind=g.Events.on;g.Events.unbind=g.Events.off;g.Model=function(a,b){var c;a||(a={});b&&b.parse&&(a=this.parse(a));if(c=j(this,"defaults"))a=f.extend({},c,a);if(b&&b.collection)this.collection=b.collection;this.attributes={};this._escapedAttributes={};this.cid=f.uniqueId("c");this._changed={};if(!this.set(a,{silent:true}))throw Error("Can't create an invalid model");
-this._changed={};this._previousAttributes=f.clone(this.attributes);this.initialize.apply(this,arguments)};f.extend(g.Model.prototype,g.Events,{idAttribute:"id",initialize:function(){},toJSON:function(){return f.clone(this.attributes)},get:function(a){return this.attributes[a]},escape:function(a){var b;if(b=this._escapedAttributes[a])return b;b=this.attributes[a];return this._escapedAttributes[a]=f.escape(b==null?"":""+b)},has:function(a){return this.attributes[a]!=null},set:function(a,b,c){var d,
-e;f.isObject(a)||a==null?(d=a,c=b):(d={},d[a]=b);c||(c={});if(!d)return this;if(d instanceof g.Model)d=d.attributes;if(c.unset)for(e in d)d[e]=void 0;if(this.validate&&!this._performValidation(d,c))return false;if(this.idAttribute in d)this.id=d[this.idAttribute];var b=this.attributes,k=this._escapedAttributes,n=this._previousAttributes||{},u=this._changing;this._changing=true;for(e in d)if(a=d[e],f.isEqual(b[e],a)||delete k[e],c.unset?delete b[e]:b[e]=a,delete this._changed[e],!f.isEqual(n[e],a)||
-f.has(b,e)!=f.has(n,e))this._changed[e]=a;if(!u)!c.silent&&this.hasChanged()&&this.change(c),this._changing=false;return this},unset:function(a,b){(b||(b={})).unset=true;return this.set(a,null,b)},clear:function(a){(a||(a={})).unset=true;return this.set(f.clone(this.attributes),a)},fetch:function(a){var a=a?f.clone(a):{},b=this,c=a.success;a.success=function(d,e,f){if(!b.set(b.parse(d,f),a))return false;c&&c(b,d)};a.error=g.wrapError(a.error,b,a);return(this.sync||g.sync).call(this,"read",this,a)},
-save:function(a,b,c){var d;f.isObject(a)||a==null?(d=a,c=b):(d={},d[a]=b);c=c?f.clone(c):{};if(d&&!this[c.wait?"_performValidation":"set"](d,c))return false;var e=this,k=c.success;c.success=function(a,b,g){b=e.parse(a,g);c.wait&&(b=f.extend(d||{},b));if(!e.set(b,c))return false;k?k(e,a):e.trigger("sync",e,a,c)};c.error=g.wrapError(c.error,e,c);a=this.isNew()?"create":"update";return(this.sync||g.sync).call(this,a,this,c)},destroy:function(a){var a=a?f.clone(a):{},b=this,c=a.success,d=function(){b.trigger("destroy",
-b,b.collection,a)};if(this.isNew())return d();a.success=function(e){a.wait&&d();c?c(b,e):b.trigger("sync",b,e,a)};a.error=g.wrapError(a.error,b,a);var e=(this.sync||g.sync).call(this,"delete",this,a);a.wait||d();return e},url:function(){var a=j(this.collection,"url")||j(this,"urlRoot")||o();return this.isNew()?a:a+(a.charAt(a.length-1)=="/"?"":"/")+encodeURIComponent(this.id)},parse:function(a){return a},clone:function(){return new this.constructor(this.attributes)},isNew:function(){return this.id==
-null},change:function(a){for(var b in this._changed)this.trigger("change:"+b,this,this._changed[b],a);this.trigger("change",this,a);this._previousAttributes=f.clone(this.attributes);this._changed={}},hasChanged:function(a){return a?f.has(this._changed,a):!f.isEmpty(this._changed)},changedAttributes:function(a){if(!a)return this.hasChanged()?f.clone(this._changed):false;var b,c=false,d=this._previousAttributes,e;for(e in a)if(!f.isEqual(d[e],b=a[e]))(c||(c={}))[e]=b;return c},previous:function(a){return!a||
-!this._previousAttributes?null:this._previousAttributes[a]},previousAttributes:function(){return f.clone(this._previousAttributes)},_performValidation:function(a,b){var c=this.validate(f.extend({},this.attributes,a),b);return c?(b.error?b.error(this,c,b):this.trigger("error",this,c,b),false):true}});g.Collection=function(a,b){b||(b={});if(b.comparator)this.comparator=b.comparator;this._reset();this.initialize.apply(this,arguments);a&&this.reset(a,{silent:true,parse:b.parse})};f.extend(g.Collection.prototype,
-g.Events,{model:g.Model,initialize:function(){},toJSON:function(){return this.map(function(a){return a.toJSON()})},add:function(a,b){var c,d,e,g,h,i={},j={};b||(b={});a=f.isArray(a)?a.slice():[a];for(c=0,d=a.length;c<d;c++){if(!(e=a[c]=this._prepareModel(a[c],b)))throw Error("Can't add an invalid model to a collection");if(i[g=e.cid]||this._byCid[g]||(h=e.id)!=null&&(j[h]||this._byId[h]))throw Error("Can't add the same model to a collection twice");i[g]=j[h]=e}for(c=0;c<d;c++)(e=a[c]).on("all",this._onModelEvent,
-this),this._byCid[e.cid]=e,e.id!=null&&(this._byId[e.id]=e);this.length+=d;t.apply(this.models,[b.at!=null?b.at:this.models.length,0].concat(a));this.comparator&&this.sort({silent:true});if(b.silent)return this;for(c=0,d=this.models.length;c<d;c++)if(i[(e=this.models[c]).cid])b.index=c,e.trigger("add",e,this,b);return this},remove:function(a,b){var c,d,e,g;b||(b={});a=f.isArray(a)?a.slice():[a];for(c=0,d=a.length;c<d;c++)if(g=this.getByCid(a[c])||this.get(a[c])){delete this._byId[g.id];delete this._byCid[g.cid];
-e=this.indexOf(g);this.models.splice(e,1);this.length--;if(!b.silent)b.index=e,g.trigger("remove",g,this,b);this._removeReference(g)}return this},get:function(a){return a==null?null: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");var b=f.bind(this.comparator,this);this.comparator.length==1?this.models=this.sortBy(b):this.models.sort(b);
-a.silent||this.trigger("reset",this,a);return this},pluck:function(a){return f.map(this.models,function(b){return b.get(a)})},reset:function(a,b){a||(a=[]);b||(b={});for(var c=0,d=this.models.length;c<d;c++)this._removeReference(this.models[c]);this._reset();this.add(a,{silent:true,parse:b.parse});b.silent||this.trigger("reset",this,b);return this},fetch:function(a){a=a?f.clone(a):{};if(a.parse===void 0)a.parse=true;var b=this,c=a.success;a.success=function(d,e,f){b[a.add?"add":"reset"](b.parse(d,
-f),a);c&&c(b,d)};a.error=g.wrapError(a.error,b,a);return(this.sync||g.sync).call(this,"read",this,a)},create:function(a,b){var c=this,b=b?f.clone(b):{},a=this._prepareModel(a,b);if(!a)return false;b.wait||c.add(a,b);var d=b.success;b.success=function(e,f){b.wait&&c.add(e,b);d?d(e,f):e.trigger("sync",a,f,b)};a.save(null,b);return a},parse:function(a){return a},chain:function(){return f(this.models).chain()},_reset:function(){this.length=0;this.models=[];this._byId={};this._byCid={}},_prepareModel:function(a,
-b){if(a instanceof g.Model){if(!a.collection)a.collection=this}else{var c;b.collection=this;a=new this.model(a,b);a.validate&&!a._performValidation(a.attributes,b)&&(a=false)}return a},_removeReference:function(a){this==a.collection&&delete a.collection;a.off("all",this._onModelEvent,this)},_onModelEvent:function(a,b,c,d){(a=="add"||a=="remove")&&c!=this||(a=="destroy"&&this.remove(b,d),b&&a==="change:"+b.idAttribute&&(delete this._byId[b.previous(b.idAttribute)],this._byId[b.id]=b),this.trigger.apply(this,
-arguments))}});f.each("forEach,each,map,reduce,reduceRight,find,detect,filter,select,reject,every,all,some,any,include,contains,invoke,max,min,sortBy,sortedIndex,toArray,size,first,initial,rest,last,without,indexOf,shuffle,lastIndexOf,isEmpty,groupBy".split(","),function(a){g.Collection.prototype[a]=function(){return f[a].apply(f,[this.models].concat(f.toArray(arguments)))}});g.Router=function(a){a||(a={});if(a.routes)this.routes=a.routes;this._bindRoutes();this.initialize.apply(this,arguments)};
-var v=/:\w+/g,w=/\*\w+/g,x=/[-[\]{}()+?.,\\^$|#\s]/g;f.extend(g.Router.prototype,g.Events,{initialize:function(){},route:function(a,b,c){g.history||(g.history=new g.History);f.isRegExp(a)||(a=this._routeToRegExp(a));c||(c=this[b]);g.history.route(a,f.bind(function(d){d=this._extractParameters(a,d);c&&c.apply(this,d);this.trigger.apply(this,["route:"+b].concat(d));g.history.trigger("route",this,b,d)},this));return this},navigate:function(a,b){g.history.navigate(a,b)},_bindRoutes:function(){if(this.routes){var a=
-[],b;for(b in this.routes)a.unshift([b,this.routes[b]]);b=0;for(var c=a.length;b<c;b++)this.route(a[b][0],a[b][1],this[a[b][1]])}},_routeToRegExp:function(a){a=a.replace(x,"\\$&").replace(v,"([^/]+)").replace(w,"(.*?)");return RegExp("^"+a+"$")},_extractParameters:function(a,b){return a.exec(b).slice(1)}});g.History=function(){this.handlers=[];f.bindAll(this,"checkUrl")};var m=/^[#\/]/,y=/msie [\w.]+/,l=false;f.extend(g.History.prototype,g.Events,{interval:50,getFragment:function(a,b){if(a==null)if(this._hasPushState||
-b){var a=window.location.pathname,c=window.location.search;c&&(a+=c)}else a=window.location.hash;a=decodeURIComponent(a.replace(m,""));a.indexOf(this.options.root)||(a=a.substr(this.options.root.length));return a},start:function(a){if(l)throw Error("Backbone.history has already been started");this.options=f.extend({},{root:"/"},this.options,a);this._wantsHashChange=this.options.hashChange!==false;this._wantsPushState=!!this.options.pushState;this._hasPushState=!(!this.options.pushState||!window.history||
-!window.history.pushState);var a=this.getFragment(),b=document.documentMode;if(b=y.exec(navigator.userAgent.toLowerCase())&&(!b||b<=7))this.iframe=h('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo("body")[0].contentWindow,this.navigate(a);if(this._hasPushState)h(window).bind("popstate",this.checkUrl);else if(this._wantsHashChange&&"onhashchange"in window&&!b)h(window).bind("hashchange",this.checkUrl);else if(this._wantsHashChange)this._checkUrlInterval=setInterval(this.checkUrl,this.interval);
-this.fragment=a;l=true;a=window.location;b=a.pathname==this.options.root;if(this._wantsHashChange&&this._wantsPushState&&!this._hasPushState&&!b)return this.fragment=this.getFragment(null,true),window.location.replace(this.options.root+"#"+this.fragment),true;else if(this._wantsPushState&&this._hasPushState&&b&&a.hash)this.fragment=a.hash.replace(m,""),window.history.replaceState({},document.title,a.protocol+"//"+a.host+this.options.root+this.fragment);if(!this.options.silent)return this.loadUrl()},
-stop:function(){h(window).unbind("popstate",this.checkUrl).unbind("hashchange",this.checkUrl);clearInterval(this._checkUrlInterval);l=false},route:function(a,b){this.handlers.unshift({route:a,callback:b})},checkUrl:function(){var a=this.getFragment();a==this.fragment&&this.iframe&&(a=this.getFragment(this.iframe.location.hash));if(a==this.fragment||a==decodeURIComponent(this.fragment))return false;this.iframe&&this.navigate(a);this.loadUrl()||this.loadUrl(window.location.hash)},loadUrl:function(a){var b=
-this.fragment=this.getFragment(a);return f.any(this.handlers,function(a){if(a.route.test(b))return a.callback(b),true})},navigate:function(a,b){if(!l)return false;if(!b||b===true)b={trigger:b};var c=(a||"").replace(m,"");if(!(this.fragment==c||this.fragment==decodeURIComponent(c)))this._hasPushState?(c.indexOf(this.options.root)!=0&&(c=this.options.root+c),this.fragment=c,window.history[b.replace?"replaceState":"pushState"]({},document.title,c)):this._wantsHashChange?(this.fragment=c,this._updateHash(window.location,
-c,b.replace),this.iframe&&c!=this.getFragment(this.iframe.location.hash)&&(b.replace||this.iframe.document.open().close(),this._updateHash(this.iframe.location,c,b.replace))):window.location.assign(this.options.root+a),b.trigger&&this.loadUrl(a)},_updateHash:function(a,b,c){c?a.replace(a.toString().replace(/(javascript:|#).*$/,"")+"#"+b):a.hash=b}});g.View=function(a){this.cid=f.uniqueId("view");this._configure(a||{});this._ensureElement();this.initialize.apply(this,arguments);this.delegateEvents()};
-var z=/^(\S+)\s*(.*)$/,p="model,collection,el,id,attributes,className,tagName".split(",");f.extend(g.View.prototype,g.Events,{tagName:"div",$:function(a){return this.$el.find(a)},initialize:function(){},render:function(){return this},remove:function(){this.$el.remove();return this},make:function(a,b,c){a=document.createElement(a);b&&h(a).attr(b);c&&h(a).html(c);return a},setElement:function(a,b){this.$el=h(a);this.el=this.$el[0];b!==false&&this.delegateEvents()},delegateEvents:function(a){if(a||(a=
-j(this,"events"))){this.undelegateEvents();for(var b in a){var c=a[b];f.isFunction(c)||(c=this[a[b]]);if(!c)throw Error('Event "'+a[b]+'" does not exist');var d=b.match(z),e=d[1],d=d[2],c=f.bind(c,this);e+=".delegateEvents"+this.cid;d===""?this.$el.bind(e,c):this.$el.delegate(d,e,c)}}},undelegateEvents:function(){this.$el.unbind(".delegateEvents"+this.cid)},_configure:function(a){this.options&&(a=f.extend({},this.options,a));for(var b=0,c=p.length;b<c;b++){var d=p[b];a[d]&&(this[d]=a[d])}this.options=
-a},_ensureElement:function(){if(this.el)this.setElement(this.el,false);else{var a=j(this,"attributes")||{};if(this.id)a.id=this.id;if(this.className)a["class"]=this.className;this.setElement(this.make(this.tagName,a),false)}}});g.Model.extend=g.Collection.extend=g.Router.extend=g.View.extend=function(a,b){var c=A(this,a,b);c.extend=this.extend;return c};var B={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};g.sync=function(a,b,c){var d=B[a],e={type:d,dataType:"json"};if(!c.url)e.url=j(b,
-"url")||o();if(!c.data&&b&&(a=="create"||a=="update"))e.contentType="application/json",e.data=JSON.stringify(b.toJSON());if(g.emulateJSON)e.contentType="application/x-www-form-urlencoded",e.data=e.data?{model:e.data}:{};if(g.emulateHTTP&&(d==="PUT"||d==="DELETE")){if(g.emulateJSON)e.data._method=d;e.type="POST";e.beforeSend=function(a){a.setRequestHeader("X-HTTP-Method-Override",d)}}if(e.type!=="GET"&&!g.emulateJSON)e.processData=false;return h.ajax(f.extend(e,c))};g.wrapError=function(a,b,c){return function(d,
-e){e=d===b?e:d;a?a(d,e,c):b.trigger("error",d,e,c)}};var q=function(){},A=function(a,b,c){var d;d=b&&b.hasOwnProperty("constructor")?b.constructor:function(){a.apply(this,arguments)};f.extend(d,a);q.prototype=a.prototype;d.prototype=new q;b&&f.extend(d.prototype,b);c&&f.extend(d,c);d.prototype.constructor=d;d.__super__=a.prototype;return d},j=function(a,b){return!a||!a[b]?null:f.isFunction(a[b])?a[b]():a[b]},o=function(){throw Error('A "url" property or function must be specified');}}).call(this);
+(function(){var i=this,r=i.Backbone,s=Array.prototype.slice,t=Array.prototype.splice,g;g="undefined"!==typeof exports?exports:i.Backbone={};g.VERSION="0.9.1";var f=i._;!f&&"undefined"!==typeof require&&(f=require("underscore"));var h=i.jQuery||i.Zepto||i.ender;g.setDomLibrary=function(a){h=a};g.noConflict=function(){i.Backbone=r;return this};g.emulateHTTP=!1;g.emulateJSON=!1;g.Events={on:function(a,b,c){for(var d,a=a.split(/\s+/),e=this._callbacks||(this._callbacks={});d=a.shift();){d=e[d]||(e[d]=
+{});var f=d.tail||(d.tail=d.next={});f.callback=b;f.context=c;d.tail=f.next={}}return this},off:function(a,b,c){var d,e,f;if(a){if(e=this._callbacks)for(a=a.split(/\s+/);d=a.shift();)if(f=e[d],delete e[d],b&&f)for(;(f=f.next)&&f.next;)if(!(f.callback===b&&(!c||f.context===c)))this.on(d,f.callback,f.context)}else delete this._callbacks;return this},trigger:function(a){var b,c,d,e;if(!(d=this._callbacks))return this;e=d.all;for((a=a.split(/\s+/)).push(null);b=a.shift();)e&&a.push({next:e.next,tail:e.tail,
+event:b}),(c=d[b])&&a.push({next:c.next,tail:c.tail});for(e=s.call(arguments,1);c=a.pop();){b=c.tail;for(d=c.event?[c.event].concat(e):e;(c=c.next)!==b;)c.callback.apply(c.context||this,d)}return this}};g.Events.bind=g.Events.on;g.Events.unbind=g.Events.off;g.Model=function(a,b){var c;a||(a={});b&&b.parse&&(a=this.parse(a));if(c=j(this,"defaults"))a=f.extend({},c,a);b&&b.collection&&(this.collection=b.collection);this.attributes={};this._escapedAttributes={};this.cid=f.uniqueId("c");if(!this.set(a,
+{silent:!0}))throw Error("Can't create an invalid model");delete this._changed;this._previousAttributes=f.clone(this.attributes);this.initialize.apply(this,arguments)};f.extend(g.Model.prototype,g.Events,{idAttribute:"id",initialize:function(){},toJSON:function(){return f.clone(this.attributes)},get:function(a){return this.attributes[a]},escape:function(a){var b;if(b=this._escapedAttributes[a])return b;b=this.attributes[a];return this._escapedAttributes[a]=f.escape(null==b?"":""+b)},has:function(a){return null!=
+this.attributes[a]},set:function(a,b,c){var d,e;f.isObject(a)||null==a?(d=a,c=b):(d={},d[a]=b);c||(c={});if(!d)return this;d instanceof g.Model&&(d=d.attributes);if(c.unset)for(e in d)d[e]=void 0;if(!this._validate(d,c))return!1;this.idAttribute in d&&(this.id=d[this.idAttribute]);var b=this.attributes,k=this._escapedAttributes,n=this._previousAttributes||{},h=this._setting;this._changed||(this._changed={});this._setting=!0;for(e in d)if(a=d[e],f.isEqual(b[e],a)||delete k[e],c.unset?delete b[e]:b[e]=
+a,this._changing&&!f.isEqual(this._changed[e],a)&&(this.trigger("change:"+e,this,a,c),this._moreChanges=!0),delete this._changed[e],!f.isEqual(n[e],a)||f.has(b,e)!=f.has(n,e))this._changed[e]=a;h||(!c.silent&&this.hasChanged()&&this.change(c),this._setting=!1);return this},unset:function(a,b){(b||(b={})).unset=!0;return this.set(a,null,b)},clear:function(a){(a||(a={})).unset=!0;return this.set(f.clone(this.attributes),a)},fetch:function(a){var a=a?f.clone(a):{},b=this,c=a.success;a.success=function(d,
+e,f){if(!b.set(b.parse(d,f),a))return!1;c&&c(b,d)};a.error=g.wrapError(a.error,b,a);return(this.sync||g.sync).call(this,"read",this,a)},save:function(a,b,c){var d,e;f.isObject(a)||null==a?(d=a,c=b):(d={},d[a]=b);c=c?f.clone(c):{};c.wait&&(e=f.clone(this.attributes));a=f.extend({},c,{silent:!0});if(d&&!this.set(d,c.wait?a:c))return!1;var k=this,h=c.success;c.success=function(a,b,e){b=k.parse(a,e);c.wait&&(b=f.extend(d||{},b));if(!k.set(b,c))return!1;h?h(k,a):k.trigger("sync",k,a,c)};c.error=g.wrapError(c.error,
+k,c);b=this.isNew()?"create":"update";b=(this.sync||g.sync).call(this,b,this,c);c.wait&&this.set(e,a);return b},destroy:function(a){var a=a?f.clone(a):{},b=this,c=a.success,d=function(){b.trigger("destroy",b,b.collection,a)};if(this.isNew())return d();a.success=function(e){a.wait&&d();c?c(b,e):b.trigger("sync",b,e,a)};a.error=g.wrapError(a.error,b,a);var e=(this.sync||g.sync).call(this,"delete",this,a);a.wait||d();return e},url:function(){var a=j(this.collection,"url")||j(this,"urlRoot")||o();return this.isNew()?
+a:a+("/"==a.charAt(a.length-1)?"":"/")+encodeURIComponent(this.id)},parse:function(a){return a},clone:function(){return new this.constructor(this.attributes)},isNew:function(){return null==this.id},change:function(a){if(this._changing||!this.hasChanged())return this;this._moreChanges=this._changing=!0;for(var b in this._changed)this.trigger("change:"+b,this,this._changed[b],a);for(;this._moreChanges;)this._moreChanges=!1,this.trigger("change",this,a);this._previousAttributes=f.clone(this.attributes);
+delete this._changed;this._changing=!1;return this},hasChanged:function(a){return!arguments.length?!f.isEmpty(this._changed):this._changed&&f.has(this._changed,a)},changedAttributes:function(a){if(!a)return this.hasChanged()?f.clone(this._changed):!1;var b,c=!1,d=this._previousAttributes,e;for(e in a)if(!f.isEqual(d[e],b=a[e]))(c||(c={}))[e]=b;return c},previous:function(a){return!arguments.length||!this._previousAttributes?null:this._previousAttributes[a]},previousAttributes:function(){return f.clone(this._previousAttributes)},
+isValid:function(){return!this.validate(this.attributes)},_validate:function(a,b){if(b.silent||!this.validate)return!0;var a=f.extend({},this.attributes,a),c=this.validate(a,b);if(!c)return!0;b&&b.error?b.error(this,c,b):this.trigger("error",this,c,b);return!1}});g.Collection=function(a,b){b||(b={});b.comparator&&(this.comparator=b.comparator);this._reset();this.initialize.apply(this,arguments);a&&this.reset(a,{silent:!0,parse:b.parse})};f.extend(g.Collection.prototype,g.Events,{model:g.Model,initialize:function(){},
+toJSON:function(){return this.map(function(a){return a.toJSON()})},add:function(a,b){var c,d,e,g,h,i={},j={};b||(b={});a=f.isArray(a)?a.slice():[a];for(c=0,d=a.length;c<d;c++){if(!(e=a[c]=this._prepareModel(a[c],b)))throw Error("Can't add an invalid model to a collection");if(i[g=e.cid]||this._byCid[g]||null!=(h=e.id)&&(j[h]||this._byId[h]))throw Error("Can't add the same model to a collection twice");i[g]=j[h]=e}for(c=0;c<d;c++)(e=a[c]).on("all",this._onModelEvent,this),this._byCid[e.cid]=e,null!=
+e.id&&(this._byId[e.id]=e);this.length+=d;t.apply(this.models,[null!=b.at?b.at:this.models.length,0].concat(a));this.comparator&&this.sort({silent:!0});if(b.silent)return this;for(c=0,d=this.models.length;c<d;c++)if(i[(e=this.models[c]).cid])b.index=c,e.trigger("add",e,this,b);return this},remove:function(a,b){var c,d,e,g;b||(b={});a=f.isArray(a)?a.slice():[a];for(c=0,d=a.length;c<d;c++)if(g=this.getByCid(a[c])||this.get(a[c]))delete this._byId[g.id],delete this._byCid[g.cid],e=this.indexOf(g),this.models.splice(e,
+1),this.length--,b.silent||(b.index=e,g.trigger("remove",g,this,b)),this._removeReference(g);return this},get:function(a){return null==a?null:this._byId[null!=a.id?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");var b=f.bind(this.comparator,this);1==this.comparator.length?this.models=this.sortBy(b):this.models.sort(b);a.silent||this.trigger("reset",
+this,a);return this},pluck:function(a){return f.map(this.models,function(b){return b.get(a)})},reset:function(a,b){a||(a=[]);b||(b={});for(var c=0,d=this.models.length;c<d;c++)this._removeReference(this.models[c]);this._reset();this.add(a,{silent:!0,parse:b.parse});b.silent||this.trigger("reset",this,b);return this},fetch:function(a){a=a?f.clone(a):{};void 0===a.parse&&(a.parse=!0);var b=this,c=a.success;a.success=function(d,e,f){b[a.add?"add":"reset"](b.parse(d,f),a);c&&c(b,d)};a.error=g.wrapError(a.error,
+b,a);return(this.sync||g.sync).call(this,"read",this,a)},create:function(a,b){var c=this,b=b?f.clone(b):{},a=this._prepareModel(a,b);if(!a)return!1;b.wait||c.add(a,b);var d=b.success;b.success=function(e,f){b.wait&&c.add(e,b);d?d(e,f):e.trigger("sync",a,f,b)};a.save(null,b);return a},parse:function(a){return a},chain:function(){return f(this.models).chain()},_reset:function(){this.length=0;this.models=[];this._byId={};this._byCid={}},_prepareModel:function(a,b){a instanceof g.Model?a.collection||
+(a.collection=this):(b.collection=this,a=new this.model(a,b),a._validate(a.attributes,b)||(a=!1));return a},_removeReference:function(a){this==a.collection&&delete a.collection;a.off("all",this._onModelEvent,this)},_onModelEvent:function(a,b,c,d){("add"==a||"remove"==a)&&c!=this||("destroy"==a&&this.remove(b,d),b&&a==="change:"+b.idAttribute&&(delete this._byId[b.previous(b.idAttribute)],this._byId[b.id]=b),this.trigger.apply(this,arguments))}});f.each("forEach,each,map,reduce,reduceRight,find,detect,filter,select,reject,every,all,some,any,include,contains,invoke,max,min,sortBy,sortedIndex,toArray,size,first,initial,rest,last,without,indexOf,shuffle,lastIndexOf,isEmpty,groupBy".split(","),
+function(a){g.Collection.prototype[a]=function(){return f[a].apply(f,[this.models].concat(f.toArray(arguments)))}});g.Router=function(a){a||(a={});a.routes&&(this.routes=a.routes);this._bindRoutes();this.initialize.apply(this,arguments)};var u=/:\w+/g,v=/\*\w+/g,w=/[-[\]{}()+?.,\\^$|#\s]/g;f.extend(g.Router.prototype,g.Events,{initialize:function(){},route:function(a,b,c){g.history||(g.history=new g.History);f.isRegExp(a)||(a=this._routeToRegExp(a));c||(c=this[b]);g.history.route(a,f.bind(function(d){d=
+this._extractParameters(a,d);c&&c.apply(this,d);this.trigger.apply(this,["route:"+b].concat(d));g.history.trigger("route",this,b,d)},this));return this},navigate:function(a,b){g.history.navigate(a,b)},_bindRoutes:function(){if(this.routes){var a=[],b;for(b in this.routes)a.unshift([b,this.routes[b]]);b=0;for(var c=a.length;b<c;b++)this.route(a[b][0],a[b][1],this[a[b][1]])}},_routeToRegExp:function(a){a=a.replace(w,"\\$&").replace(u,"([^/]+)").replace(v,"(.*?)");return RegExp("^"+a+"$")},_extractParameters:function(a,
+b){return a.exec(b).slice(1)}});g.History=function(){this.handlers=[];f.bindAll(this,"checkUrl")};var m=/^[#\/]/,x=/msie [\w.]+/,l=!1;f.extend(g.History.prototype,g.Events,{interval:50,getFragment:function(a,b){if(null==a)if(this._hasPushState||b){var a=window.location.pathname,c=window.location.search;c&&(a+=c)}else a=window.location.hash;a=decodeURIComponent(a);a.indexOf(this.options.root)||(a=a.substr(this.options.root.length));return a.replace(m,"")},start:function(a){if(l)throw Error("Backbone.history has already been started");
+this.options=f.extend({},{root:"/"},this.options,a);this._wantsHashChange=!1!==this.options.hashChange;this._wantsPushState=!!this.options.pushState;this._hasPushState=!(!this.options.pushState||!window.history||!window.history.pushState);var a=this.getFragment(),b=document.documentMode;if(b=x.exec(navigator.userAgent.toLowerCase())&&(!b||7>=b))this.iframe=h('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo("body")[0].contentWindow,this.navigate(a);this._hasPushState?h(window).bind("popstate",
+this.checkUrl):this._wantsHashChange&&"onhashchange"in window&&!b?h(window).bind("hashchange",this.checkUrl):this._wantsHashChange&&(this._checkUrlInterval=setInterval(this.checkUrl,this.interval));this.fragment=a;l=!0;a=window.location;b=a.pathname==this.options.root;if(this._wantsHashChange&&this._wantsPushState&&!this._hasPushState&&!b)return this.fragment=this.getFragment(null,!0),window.location.replace(this.options.root+"#"+this.fragment),!0;this._wantsPushState&&this._hasPushState&&b&&a.hash&&
+(this.fragment=a.hash.replace(m,""),window.history.replaceState({},document.title,a.protocol+"//"+a.host+this.options.root+this.fragment));if(!this.options.silent)return this.loadUrl()},stop:function(){h(window).unbind("popstate",this.checkUrl).unbind("hashchange",this.checkUrl);clearInterval(this._checkUrlInterval);l=!1},route:function(a,b){this.handlers.unshift({route:a,callback:b})},checkUrl:function(){var a=this.getFragment();a==this.fragment&&this.iframe&&(a=this.getFragment(this.iframe.location.hash));
+if(a==this.fragment||a==decodeURIComponent(this.fragment))return!1;this.iframe&&this.navigate(a);this.loadUrl()||this.loadUrl(window.location.hash)},loadUrl:function(a){var b=this.fragment=this.getFragment(a);return f.any(this.handlers,function(a){if(a.route.test(b))return a.callback(b),!0})},navigate:function(a,b){if(!l)return!1;if(!b||!0===b)b={trigger:b};var c=(a||"").replace(m,"");this.fragment==c||this.fragment==decodeURIComponent(c)||(this._hasPushState?(0!=c.indexOf(this.options.root)&&(c=
+this.options.root+c),this.fragment=c,window.history[b.replace?"replaceState":"pushState"]({},document.title,c)):this._wantsHashChange?(this.fragment=c,this._updateHash(window.location,c,b.replace),this.iframe&&c!=this.getFragment(this.iframe.location.hash)&&(b.replace||this.iframe.document.open().close(),this._updateHash(this.iframe.location,c,b.replace))):window.location.assign(this.options.root+a),b.trigger&&this.loadUrl(a))},_updateHash:function(a,b,c){c?a.replace(a.toString().replace(/(javascript:|#).*$/,
+"")+"#"+b):a.hash=b}});g.View=function(a){this.cid=f.uniqueId("view");this._configure(a||{});this._ensureElement();this.initialize.apply(this,arguments);this.delegateEvents()};var y=/^(\S+)\s*(.*)$/,p="model,collection,el,id,attributes,className,tagName".split(",");f.extend(g.View.prototype,g.Events,{tagName:"div",$:function(a){return this.$el.find(a)},initialize:function(){},render:function(){return this},remove:function(){this.$el.remove();return this},make:function(a,b,c){a=document.createElement(a);
+b&&h(a).attr(b);c&&h(a).html(c);return a},setElement:function(a,b){this.$el=h(a);this.el=this.$el[0];!1!==b&&this.delegateEvents();return this},delegateEvents:function(a){if(a||(a=j(this,"events"))){this.undelegateEvents();for(var b in a){var c=a[b];f.isFunction(c)||(c=this[a[b]]);if(!c)throw Error('Event "'+a[b]+'" does not exist');var d=b.match(y),e=d[1],d=d[2],c=f.bind(c,this),e=e+(".delegateEvents"+this.cid);""===d?this.$el.bind(e,c):this.$el.delegate(d,e,c)}}},undelegateEvents:function(){this.$el.unbind(".delegateEvents"+
+this.cid)},_configure:function(a){this.options&&(a=f.extend({},this.options,a));for(var b=0,c=p.length;b<c;b++){var d=p[b];a[d]&&(this[d]=a[d])}this.options=a},_ensureElement:function(){if(this.el)this.setElement(this.el,!1);else{var a=j(this,"attributes")||{};this.id&&(a.id=this.id);this.className&&(a["class"]=this.className);this.setElement(this.make(this.tagName,a),!1)}}});g.Model.extend=g.Collection.extend=g.Router.extend=g.View.extend=function(a,b){var c=z(this,a,b);c.extend=this.extend;return c};
+var A={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};g.sync=function(a,b,c){var d=A[a],e={type:d,dataType:"json"};c.url||(e.url=j(b,"url")||o());if(!c.data&&b&&("create"==a||"update"==a))e.contentType="application/json",e.data=JSON.stringify(b.toJSON());g.emulateJSON&&(e.contentType="application/x-www-form-urlencoded",e.data=e.data?{model:e.data}:{});if(g.emulateHTTP&&("PUT"===d||"DELETE"===d))g.emulateJSON&&(e.data._method=d),e.type="POST",e.beforeSend=function(a){a.setRequestHeader("X-HTTP-Method-Override",
+d)};"GET"!==e.type&&!g.emulateJSON&&(e.processData=!1);return h.ajax(f.extend(e,c))};g.wrapError=function(a,b,c){return function(d,e){e=d===b?e:d;a?a(b,e,c):b.trigger("error",b,e,c)}};var q=function(){},z=function(a,b,c){var d;d=b&&b.hasOwnProperty("constructor")?b.constructor:function(){a.apply(this,arguments)};f.extend(d,a);q.prototype=a.prototype;d.prototype=new q;b&&f.extend(d.prototype,b);c&&f.extend(d,c);d.prototype.constructor=d;d.__super__=a.prototype;return d},j=function(a,b){return!a||!a[b]?
+null:f.isFunction(a[b])?a[b]():a[b]},o=function(){throw Error('A "url" property or function must be specified');}}).call(this);
View
104 backbone.js
@@ -1,4 +1,5 @@
-// Backbone.js 0.9.0
+// Backbone.js 0.9.1
+
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
@@ -31,7 +32,7 @@
}
// Current version of the library. Keep in sync with `package.json`.
- Backbone.VERSION = '0.9.0-SP';
+ Backbone.VERSION = '0.9.1-SP';
// Require Underscore, if we're on the server, and it's not already present.
var _ = root._;
@@ -41,6 +42,15 @@
// var $ = root.jQuery || root.Zepto || root.ender;
var $ = (root.Sponsorpay && root.Sponsorpay.jQuery) ? root.Sponsorpay.jQuery : root.jQuery;
+ // Set the JavaScript library that will be used for DOM manipulation and
+ // Ajax calls (a.k.a. the `$` variable). By default Backbone will use: jQuery,
+ // Zepto, or Ender; but the `setDomLibrary()` method lets you inject an
+ // alternate JavaScript library (or a mock library for testing your views
+ // outside of a browser).
+ Backbone.setDomLibrary = function(lib) {
+ $ = lib;
+ };
+
// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
// to its previous owner. Returns a reference to this Backbone object.
Backbone.noConflict = function() {
@@ -164,11 +174,8 @@
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
- this._changed = {};
- if (!this.set(attributes, {silent: true})) {
- throw new Error("Can't create an invalid model");
- }
- this._changed = {};
+ this.set(attributes, {silent: true});
+ delete this._changed;
this._previousAttributes = _.clone(this.attributes);
this.initialize.apply(this, arguments);
};
@@ -227,7 +234,7 @@
if (options.unset) for (attr in attrs) attrs[attr] = void 0;
// Run validation.
- if (this.validate && !this._performValidation(attrs, options)) return false;
+ if (!this._validate(attrs, options)) return false;
// Check for changes of `id`.
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
@@ -235,14 +242,19 @@
var now = this.attributes;
var escaped = this._escapedAttributes;
var prev = this._previousAttributes || {};
- var alreadyChanging = this._changing;
- this._changing = true;
+ var alreadySetting = this._setting;
+ this._changed || (this._changed = {});
+ this._setting = true;
// Update attributes.
for (attr in attrs) {
val = attrs[attr];
if (!_.isEqual(now[attr], val)) delete escaped[attr];
options.unset ? delete now[attr] : now[attr] = val;
+ if (this._changing && !_.isEqual(this._changed[attr], val)) {
+ this.trigger('change:' + attr, this, val, options);
+ this._moreChanges = true;
+ }
delete this._changed[attr];
if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) {
this._changed[attr] = val;
@@ -250,9 +262,9 @@
}
// Fire the `"change"` events, if the model has been changed.
- if (!alreadyChanging) {
+ if (!alreadySetting) {
if (!options.silent && this.hasChanged()) this.change(options);
- this._changing = false;
+ this._setting = false;
}
return this;
},
@@ -290,7 +302,7 @@
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save: function(key, value, options) {
- var attrs;
+ var attrs, current;
if (_.isObject(key) || key == null) {
attrs = key;
options = value;
@@ -300,7 +312,11 @@
}
options = options ? _.clone(options) : {};
- if (attrs && !this[options.wait ? '_performValidation' : 'set'](attrs, options)) return false;
+ if (options.wait) current = _.clone(this.attributes);
+ var silentOptions = _.extend({}, options, {silent: true});
+ if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
+ return false;
+ }
var model = this;
var success = options.success;
options.success = function(resp, status, xhr) {
@@ -315,7 +331,9 @@
};
options.error = Backbone.wrapError(options.error, model, options);
var method = this.isNew() ? 'create' : 'update';
- return (this.sync || Backbone.sync).call(this, method, this, options);
+ var xhr = (this.sync || Backbone.sync).call(this, method, this, options);
+ if (options.wait) this.set(current, silentOptions);
+ return xhr;
},
// Destroy this model on the server if it was already persisted.
@@ -374,19 +392,27 @@
// a `"change:attribute"` event for each changed attribute.
// Calling this will cause all objects observing the model to update.
change: function(options) {
+ if (this._changing || !this.hasChanged()) return this;
+ this._changing = true;
+ this._moreChanges = true;
for (var attr in this._changed) {
this.trigger('change:' + attr, this, this._changed[attr], options);
}
- this.trigger('change', this, options);
+ while (this._moreChanges) {
+ this._moreChanges = false;
+ this.trigger('change', this, options);
+ }
this._previousAttributes = _.clone(this.attributes);
- this._changed = {};
+ delete this._changed;
+ this._changing = false;
+ return this;
},
// Determine if the model has changed since the last `"change"` event.
// If you specify an attribute name, determine if that attribute has changed.
hasChanged: function(attr) {
- if (attr) return _.has(this._changed, attr);
- return !_.isEmpty(this._changed);
+ if (!arguments.length) return !_.isEmpty(this._changed);
+ return this._changed && _.has(this._changed, attr);
},
// Return an object containing all the attributes that have changed, or
@@ -408,7 +434,7 @@
// Get the previous value of an attribute, recorded at the time the last
// `"change"` event was fired.
previous: function(attr) {
- if (!attr || !this._previousAttributes) return null;
+ if (!arguments.length || !this._previousAttributes) return null;
return this._previousAttributes[attr];
},
@@ -418,21 +444,26 @@
return _.clone(this._previousAttributes);
},
+ // Check if the model is currently in a valid state. It's only possible to
+ // get into an *invalid* state if you're using silent changes.
+ isValid: function() {
+ return !this.validate(this.attributes);
+ },
+
// Run validation against a set of incoming attributes, returning `true`
// if all is well. If a specific `error` callback has been passed,
// call that instead of firing the general `"error"` event.
- _performValidation: function(attrs, options) {
- var newAttrs = _.extend({}, this.attributes, attrs);
- var error = this.validate(newAttrs, options);
- if (error) {
- if (options.error) {
- options.error(this, error, options);
- } else {
- this.trigger('error', this, error, options);
- }
- return false;
+ _validate: function(attrs, options) {
+ if (options.silent || !this.validate) return true;
+ attrs = _.extend({}, this.attributes, attrs);
+ var error = this.validate(attrs, options);
+ if (!error) return true;
+ if (options && options.error) {
+ options.error(this, error, options);
+ } else {
+ this.trigger('error', this, error, options);
}
- return true;
+ return false;
}
});
@@ -651,7 +682,7 @@
var attrs = model;
options.collection = this;
model = new this.model(attrs, options);
- if (model.validate && !model._performValidation(model.attributes, options)) model = false;
+ if (!model._validate(model.attributes, options)) model = false;
} else if (!model.collection) {
model.collection = this;
}
@@ -816,9 +847,9 @@
fragment = window.location.hash;
}
}
- fragment = decodeURIComponent(fragment.replace(routeStripper, ''));
+ fragment = decodeURIComponent(fragment);
if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length);
- return fragment;
+ return fragment.replace(routeStripper, '');
},
// Start the hash change handling, returning `true` if the current URL matches
@@ -1032,6 +1063,7 @@
this.$el = $(element);
this.el = this.$el[0];
if (delegate !== false) this.delegateEvents();
+ return this;
},
// Set callbacks, where `this.events` is a hash of
@@ -1190,9 +1222,9 @@
return function(model, resp) {
resp = model === originalModel ? resp : model;
if (onError) {
- onError(model, resp, options);
+ onError(originalModel, resp, options);
} else {
- originalModel.trigger('error', model, resp, options);
+ originalModel.trigger('error', originalModel, resp, options);
}
};
};
View
339 docs/backbone.html
183 additions, 156 deletions not shown
View
BIN  docs/images/trajectory.png
Deleted file not rendered
View
BIN  docs/images/wunderkit.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
128 index.html
@@ -198,7 +198,7 @@
<div id="sidebar" class="interface">
<a class="toc_title" href="#">
- Backbone.js <span class="version">(0.9.0)</span>
+ Backbone.js <span class="version">(0.9.1)</span>
</a>
<a class="toc_title" href="#introduction">
@@ -240,6 +240,7 @@
<li>– <a href="#Model-save">save</a></li>
<li>– <a href="#Model-destroy">destroy</a></li>
<li>– <a href="#Model-validate">validate</a></li>
+ <li>– <a href="#Model-isValid">isValid</a></li>
<li>– <a href="#Model-url">url</a></li>
<li>– <a href="#Model-urlRoot">urlRoot</a></li>
<li>– <a href="#Model-parse">parse</a></li>
@@ -328,6 +329,7 @@
</a>
<ul class="toc_section">
<li>– <a href="#Utility-noConflict">noConflict</a></li>
+ <li>– <a href="#Utility-setDomLibrary">setDomLibrary</a></li>
</ul>
<a class="toc_title" href="#examples">
@@ -340,6 +342,7 @@
<li>– <a href="#examples-flow">Flow</a></li>
<li>– <a href="#examples-audiovroom">AudioVroom</a></li>
<li>– <a href="#examples-foursquare">Foursquare</a></li>
+ <li>- <a href="#examples-wunderkit">Wunderkit</a></li>
<li>– <a href="#examples-do">Do</a></li>
<li>– <a href="#examples-posterous">Posterous Spaces</a></li>
<li>– <a href="#examples-groupon">Groupon Now!</a></li>
@@ -397,8 +400,8 @@
</p>
<p>
- Backbone.js gives structure to your serious JavaScript web applications
- by supplying <b>models</b> with key-value binding and custom events,
+ Backbone.js gives structure to web applications
+ by providing <b>models</b> with key-value binding and custom events,
<b>collections</b> with a rich API of enumerable functions,
<b>views</b> with declarative event handling, and connects it all to your
existing API over a RESTful JSON interface.
@@ -436,11 +439,11 @@ <h2 id="downloads">
<table>
<tr>
- <td><a class="punch" href="backbone.js">Development Version (0.9.0)</a></td>
+ <td><a class="punch" href="backbone.js">Development Version (0.9.1)</a></td>
<td><i>47kb, Full source, lots of comments</i></td>
</tr>
<tr>
- <td><a class="punch" href="backbone-min.js">Production Version (0.9.0)</a></td>
+ <td><a class="punch" href="backbone-min.js">Production Version (0.9.1)</a></td>
<td><i>5.3kb, Packed and gzipped</i></td>
</tr>
</table>
@@ -479,6 +482,11 @@ <h2 id="introduction">Introduction</h2>
and update the HTML manually
&mdash; when the model changes, the views simply update themselves.
</p>
+
+ <p>
+ If you're new here, and aren't yet quite sure what Backbone is for, start by
+ browsing the <a href="#examples">list of Backbone-based projects</a>.
+ </p>
<p>
Many of the examples that follow are runnable. Click the <i>play</i> button
@@ -546,10 +554,15 @@ <h2 id="Events">Backbone.Events</h2>
<b class="header">on</b><code>object.on(event, callback, [context])</code><span class="alias">Alias: bind</span>
<br />
Bind a <b>callback</b> function to an object. The callback will be invoked
- whenever the <b>event</b> (specified by an arbitrary string identifier) is fired.
+ whenever the <b>event</b> is fired.
If you have a large number of different events on a page, the convention is to use colons to
- namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>
+ namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>.
+ The event string may also be a space-delimited list of several events...
</p>
+
+<pre>
+book.on("change:title change:author", ...);
+</pre>
<p>
To supply a <b>context</b> value for <tt>this</tt> when the callback is invoked,
@@ -591,8 +604,9 @@ <h2 id="Events">Backbone.Events</h2>
<p id="Events-trigger">
<b class="header">trigger</b><code>object.trigger(event, [*args])</code>
<br />
- Trigger callbacks for the given <b>event</b>. Subsequent arguments to
- <b>trigger</b> will be passed along to the event callbacks.
+ Trigger callbacks for the given <b>event</b>, or space-delimited list of events.
+ Subsequent arguments to <b>trigger</b> will be passed along to the
+ event callbacks.
</p>
<h2 id="Model">Backbone.Model</h2>
@@ -1028,6 +1042,15 @@ <h2 id="Model">Backbone.Model</h2>
});
</pre>
+ <p id="Model-isValid">
+ <b class="header">isValid</b><code>model.isValid()</code>
+ <br />
+ Models may enter an invalid state if you make changes to them silently
+ ... useful when dealing with form input. Call <tt>model.isValid()</tt>
+ to check if the model is currently in a valid state, according to your
+ <tt>validate</tt> function.
+ </p>
+
<p id="Model-url">
<b class="header">url</b><code>model.url()</code>
<br />
@@ -2203,10 +2226,6 @@ <h2 id="View">Backbone.View</h2>
<h2 id="Utility">Utility Functions</h2>
- <p>
-
- </p>
-
<p id="Utility-noConflict">
<b class="header">noConflict</b><code>var backbone = Backbone.noConflict();</code>
<br />
@@ -2221,7 +2240,21 @@ <h2 id="Utility">Utility Functions</h2>
var model = localBackbone.Model.extend(...);
</pre>
+ <p id="Utility-setDomLibrary">
+ <b class="header">setDomLibrary</b><code>Backbone.setDomLibrary(jQueryNew);</code>
+ <br />
+ If you have multiple copies of <tt>jQuery</tt> on the page, or simply want
+ to tell Backbone to use a particular object as it's DOM / Ajax library,
+ this is the function for you.
+ </p>
+
<h2 id="examples">Examples</h2>
+
+ <p>
+ The list of examples that follows, while long, is not exhaustive. If you've
+ worked on an app that uses Backbone, please add it to the
+ <a href="https://github.com/documentcloud/backbone/wiki/Projects-and-Companies-using-Backbone">wiki page of Backbone apps</a>.
+ </p>
<p id="examples-todos">
<a href="http://jgn.me/">Jérôme Gravel-Niquet</a> has contributed a
@@ -2327,6 +2360,27 @@ <h2 id="examples-foursquare">Foursquare</h2>
<img src="docs/images/foursquare.png" alt="Foursquare" class="example_image" />
</a>
</div>
+
+ <h2 id="examples-wunderkit">Wunderkit</h2>
+
+ <p>
+ <a href="http://get.wunderkit.com/">Wunderkit</a> is a productivity and
+ social collaboration platform. It
+ uses Backbone.js as the foundation for the single-page application,
+ which is backed by a RESTful Rails API.
+ The freedom and agility that Backbone gives to developers
+ made it possible to build Wunderkit in a very short time and
+ extend it with custom features: a write-through cache using HTML5
+ localStorage, and a view hierarchy extension to easily manage trees of
+ sub-views. Aside from Backbone, Wunderkit also
+ depends on jQuery, Underscore, Require.js, LESS and doT.js templates.
+ </p>
+
+ <div style="text-align: center;">
+ <a href="http://get.wunderkit.com/">
+ <img src="docs/images/wunderkit.png" alt="Wunderkit" class="example_image" />
+ </a>
+ </div>
<h2 id="examples-do">Do</h2>
@@ -2459,23 +2513,6 @@ <h2 id="examples-diaspora">Diaspora</h2>
</a>
</div>
- <h2 id="examples-trajectory">Trajectory</h2>
-
- <p>
- <a href="https://www.apptrajectory.com/">Trajectory</a> is an agile
- software planning tool used to discuss wireframes, record decisions made,
- relate user stories and bugs to discussions, and track your progress and
- plan the future. With Rails on the backend, Trajectory uses Backbone.js
- heavily to provide a fluid story planning interface that even updates in
- real-time based on the actions of other users.
- </p>
-
- <div style="text-align: center;">
- <a href="http://www.apptrajectory.com/">
- <img src="docs/images/trajectory.png" alt="Trajectory" class="example_image" />
- </a>
- </div>
-
<h2 id="examples-soundcloud">SoundCloud Mobile</h2>
<p>
@@ -3133,6 +3170,24 @@ <h2 id="faq">F.A.Q.</h2>
<h2 id="changelog">Change Log</h2>
+ <b class="header">0.9.1</b> &mdash; <small><i>Feb. 2, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/backbone/compare/0.9.0...0.9.1">Diff</a><br />
+ <ul style="margin-top: 5px;">
+ <li>
+ Reverted to 0.5.3-esque behavior for validating models. Silent changes
+ no longer trigger validation (making it easier to work with forms).
+ Added an <tt>isValid</tt> function that you can use to check if a model
+ is currently in a valid state.
+ </li>
+ <li>
+ If you have multiple versions of jQuery on the page, you can now tell
+ Backbone which one to use with <tt>Backbone.setDomLibrary</tt>.
+ </li>
+ <li>
+ Fixes regressions in <b>0.9.0</b> for routing with "root", saving with
+ both "wait" and "validate", and the order of nested "change" events.
+ </li>
+ </ul>
+
<b class="header">0.9.0</b> &mdash; <small><i>Jan. 30, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/backbone/compare/0.5.3...0.9.0">Diff</a><br />
<ul style="margin-top: 5px;">
<li>
@@ -3148,6 +3203,10 @@ <h2 id="changelog">Change Log</h2>
re-delegating events on the new DOM element.
</li>
<li>
+ You can now bind and trigger multiple spaced-delimited events at once.
+ For example: <tt>model.on("change:name change:age", ...)</tt>
+ </li>
+ <li>
When you don't know the key in advance, you may now call
<tt>model.set(key, value)</tt> as well as <tt>save</tt>.
</li>
@@ -3178,6 +3237,11 @@ <h2 id="changelog">Change Log</h2>
values as well as the string names of existing view methods.
</li>
<li>
+ Validation has gotten an overhaul &mdash; a model's <tt>validate</tt> function
+ will now be run even for silent changes, and you can no longer create
+ a model in an initially invalid state.
+ </li>
+ <li>
Added <tt>shuffle</tt> and <tt>initial</tt> to collections, proxied
from Underscore.
</li>
@@ -3212,6 +3276,10 @@ <h2 id="changelog">Change Log</h2>
Although you shouldn't be writing your routes with them in any case &mdash;
leading slashes (<tt>/</tt>) are now stripped from routes.
</li>
+ <li>
+ Calling <tt>clone</tt> on a model now only passes the attributes
+ for duplication, not a reference to the model itself.
+ </li>
</ul>
<p>
View
2  package.json
@@ -10,5 +10,5 @@
},
"lib" : ".",
"main" : "backbone.js",
- "version" : "0.9.0-SP"
+ "version" : "0.9.1-SP"
}
View
76 test/collection.js
@@ -1,12 +1,25 @@
$(document).ready(function() {
- module("Backbone.Collection");
-
- window.lastRequest = null;
+ var lastRequest = null;
+ var sync = Backbone.sync;
+
+ module("Backbone.Collection", {
+
+ setup: function() {
+ Backbone.sync = function(method, model, options) {
+ lastRequest = {
+ method: method,
+ model: model,
+ options: options
+ };
+ };
+ },
+
+ teardown: function() {
+ Backbone.sync = sync;
+ }
- Backbone.sync = function() {
- lastRequest = _.toArray(arguments);
- };
+ });
var a = new Backbone.Model({id: 3, label: 'a'});
var b = new Backbone.Model({id: 2, label: 'b'});
@@ -50,34 +63,6 @@ $(document).ready(function() {
equal(col.get(101), model);
});
- test("Collection: add model with attributes modified by set", function() {
- var CustomSetModel = Backbone.Model.extend({
- defaults: {
- number_as_string: null //presence of defaults forces extend
- },
-
- validate: function (attributes) {
- if (!_.isString(attributes.num_as_string)) {
- return 'fail';
- }
- },
-
- set: function (attributes, options) {
- if (attributes.num_as_string) {
- attributes.num_as_string = attributes.num_as_string.toString();
- }
- Backbone.Model.prototype.set.call(this, attributes, options);
- }
- });
-
- var CustomSetCollection = Backbone.Collection.extend({
- model: CustomSetModel
- });
- raises(function(){
- new CustomSetCollection([{ num_as_string: 2 }]);
- });
- });
-
test("Collection: update index when id changes", function() {
var col = new Backbone.Collection();
col.add([
@@ -338,18 +323,18 @@ $(document).ready(function() {
test("Collection: fetch", function() {
col.fetch();
- equal(lastRequest[0], 'read');
- equal(lastRequest[1], col);
- equal(lastRequest[2].parse, true);
+ equal(lastRequest.method, 'read');
+ equal(lastRequest.model, col);
+ equal(lastRequest.options.parse, true);
col.fetch({parse: false});
- equal(lastRequest[2].parse, false);
+ equal(lastRequest.options.parse, false);
});
test("Collection: create", function() {
var model = col.create({label: 'f'}, {wait: true});
- equal(lastRequest[0], 'create');
- equal(lastRequest[1], model);
+ equal(lastRequest.method, 'create');
+ equal(lastRequest.model, model);
equal(model.get('label'), 'f');
equal(model.collection, col);
});
@@ -364,9 +349,7 @@ $(document).ready(function() {
model: ValidatingModel
});
var col = new ValidatingCollection();
- raises(function(){
- equal(col.create({"foo":"bar"}),false);
- });
+ equal(col.create({"foo":"bar"}), false);
});
test("Collection: a failing create runs the error callback", function() {
@@ -381,9 +364,8 @@ $(document).ready(function() {
var flag = false;
var callback = function(model, error) { flag = true; };
var col = new ValidatingCollection();
- raises(function(){
- col.create({"foo":"bar"}, { error: callback });
- });
+ col.create({"foo":"bar"}, { error: callback });
+ equal(flag, true);
});
test("collection: initialize", function() {
@@ -530,7 +512,7 @@ $(document).ready(function() {
test("Collection: multiple copies of the same model", function() {
var col = new Backbone.Collection();
var model = new Backbone.Model();
- raises(function() { col.add([model, model]) });
+ raises(function() { col.add([model, model]); });
raises(function() { col.add([{id: 1}, {id: 1}]); });
});
View
170 test/model.js
@@ -1,16 +1,36 @@
$(document).ready(function() {
- module("Backbone.Model");
-
// Variable to catch the last request.
- window.lastRequest = null;
-
- window.originalSync = Backbone.sync;
+ var lastRequest = null;
+ // Variable to catch ajax params.
+ var ajaxParams = null;
+ var sync = Backbone.sync;
+ var ajax = $.ajax;
+ var urlRoot = null;
+
+ module("Backbone.Model", {
+
+ setup: function() {
+ Backbone.sync = function(method, model, options) {
+ lastRequest = {
+ method: method,
+ model: model,
+ options: options
+ };
+ sync.apply(this, arguments);
+ };
+ $.ajax = function(params) { ajaxParams = params; };
+ urlRoot = Backbone.Model.prototype.urlRoot;
+ Backbone.Model.prototype.urlRoot = '/';
+ },
+
+ teardown: function() {
+ Backbone.sync = sync;
+ $.ajax = ajax;
+ Backbone.Model.prototype.urlRoot = urlRoot;
+ }
- // Stub out Backbone.request...
- Backbone.sync = function() {
- lastRequest = _.toArray(arguments);
- };
+ });
var attrs = {
id : '1-the-tempest',
@@ -67,13 +87,8 @@ $(document).ready(function() {
doc.collection.url = '/collection/';
equal(doc.url(), '/collection/1-the-tempest');
doc.collection = null;
- var failed = false;
- try {
- doc.url();
- } catch (e) {
- failed = true;
- }
- equal(failed, true);
+ doc.urlRoot = null;
+ raises(function() { doc.url(); });
doc.collection = collection;
});
@@ -312,7 +327,7 @@ $(document).ready(function() {
var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
model.on('change', function () {
model.save();
- ok(_.isEqual(lastRequest[1], model));
+ ok(_.isEqual(lastRequest.model, model));
});
model.set({lastName: 'Hicks'});
});
@@ -332,10 +347,22 @@ $(document).ready(function() {
equal(lastError, "Can't change admin status.");
});
+ test("Model: isValid", function() {
+ var model = new Backbone.Model({valid: true});
+ model.validate = function(attrs) {
+ if (!attrs.valid) return "invalid";
+ };
+ equal(model.isValid(), true);
+ equal(model.set({valid: false}), false);
+ equal(model.isValid(), true);
+ ok(model.set('valid', false, {silent: true}));
+ equal(model.isValid(), false);
+ });
+
test("Model: save", function() {
doc.save({title : "Henry V"});
- equal(lastRequest[0], 'update');
- ok(_.isEqual(lastRequest[1], doc));
+ equal(lastRequest.method, 'update');
+ ok(_.isEqual(lastRequest.model, doc));
});
test("Model: save in positional style", function() {
@@ -349,14 +376,14 @@ $(document).ready(function() {
test("Model: fetch", function() {
doc.fetch();
- equal(lastRequest[0], 'read');
- ok(_.isEqual(lastRequest[1], doc));
+ equal(lastRequest.method, 'read');
+ ok(_.isEqual(lastRequest.model, doc));
});
test("Model: destroy", function() {
doc.destroy();
- equal(lastRequest[0], 'delete');
- ok(_.isEqual(lastRequest[1], doc));
+ equal(lastRequest.method, 'delete');
+ ok(_.isEqual(lastRequest.model, doc));
});
test("Model: non-persisted destroy", function() {
@@ -381,9 +408,9 @@ $(document).ready(function() {
equal(model.get('a'), 100);
equal(lastError, undefined);
result = model.set({admin: true}, {silent: true});
+ equal(model.get('admin'), true);
+ result = model.set({a: 200, admin: false});
equal(lastError, "Can't change admin status.");
- equal(model.get('admin'), void 0);
- result = model.set({a: 200, admin: true});
equal(result, false);
equal(model.get('a'), 100);
});
@@ -495,17 +522,6 @@ $(document).ready(function() {
a.set({state: 'hello'});
});
- test("Model: Multiple nested calls to set", function() {
- var counter = 0, model = new Backbone.Model({});
- model.on('change', function() {
- counter++;
- model.set({b: 1});
- model.set({a: 1});
- })
- .set({a: 1});
- equal(counter, 1, 'change is only triggered once');
- });
-
test("hasChanged/set should use same comparison", function() {
expect(2);
var changed = 0, model = new Backbone.Model({a: null});
@@ -583,4 +599,86 @@ $(document).ready(function() {
ok(model.hasChanged());
});
+ test("save with `wait` succeeds without `validate`", function() {
+ var model = new Backbone.Model();
+ model.save({x: 1}, {wait: true});
+ ok(lastRequest.model === model);
+ });
+
+ test("`hasChanged` for falsey keys", function() {
+ var model = new Backbone.Model();
+ model.set({x: true}, {silent: true});
+ ok(!model.hasChanged(0));
+ ok(!model.hasChanged(''));
+ });
+
+ test("`previous` for falsey keys", function() {
+ var model = new Backbone.Model({0: true, '': true});
+ model.set({0: false, '': false}, {silent: true});
+ equal(model.previous(0), true);
+ equal(model.previous(''), true);
+ });
+
+ test("`save` with `wait` sends correct attributes", function() {
+ var changed = 0;
+ var model = new Backbone.Model({x: 1, y: 2});
+ model.on('change:x', function() { changed++; });
+ model.save({x: 3}, {wait: true});
+ deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
+ equal(model.get('x'), 1);
+ equal(changed, 0);
+ lastRequest.options.success({});
+ equal(model.get('x'), 3);
+ equal(changed, 1);
+ });
+
+ test("nested `set` during `'change:attr'`", 1, function() {
+ var model = new Backbone.Model();
+ model.on('change:x', function() { ok(true); });
+ model.on('change:y', function() {
+ model.set({x: true});
+ // only fires once
+ model.set({x: true});
+ });
+ model.set({y: true});
+ });
+
+ test("nested `change` only fires once", 1, function() {
+ var model = new Backbone.Model();
+ model.on('change', function() {
+ ok(true);
+ model.change();
+ });
+ model.set({x: true});
+ });
+
+ test("no `'change'` event if no changes", function() {
+ var model = new Backbone.Model();
+ model.on('change', function() { ok(false); });
+ model.change();
+ });
+
+ test("nested `set` suring `'change'`", 3, function() {
+ var count = 0;
+ var model = new Backbone.Model();
+ model.on('change', function() {
+ switch(count++) {
+ case 0:
+ deepEqual(this.changedAttributes(), {x: true});
+ model.set({y: true});
+ break;
+ case 1:
+ deepEqual(this.changedAttributes(), {x: true, y: true});
+ model.set({z: true});
+ break;
+ case 2:
+ deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
+ break;
+ default:
+ ok(false);
+ }
+ });
+ model.set({x: true});
+ });
+
});
View
31 test/setdomlibrary.js
@@ -0,0 +1,31 @@
+$(document).ready(function(jQuery) {
+
+ // a mock object that looks sufficiently jQuery-ish
+ var myLib = function() {
+ return {
+ attr: function() { return "spam" },
+ html: function() { return "spam" },
+ hasClass: function() { return "spam" }
+ };
+ };
+
+ var viewAttrs = { id: 'test-setdomlibrary', className: 'test-setdomlibrary' }
+
+ module("Backbone.setDomLibrary", {
+
+ teardown: function() {
+ Backbone.setDomLibrary(jQuery);
+ }
+
+ });
+
+ test('Changing jQuery library to custom library', function() {
+ Backbone.setDomLibrary(myLib);
+ var view = new Backbone.View(viewAttrs);
+ ok(view.$el.hasClass('test-setdomlibrary') === 'spam');
+ Backbone.setDomLibrary(jQuery);
+ var view = new Backbone.View(viewAttrs);
+ ok(view.$el.hasClass('test-setdomlibrary'));
+ });
+
+});
View
23 test/sync.js
@@ -1,11 +1,21 @@
$(document).ready(function() {
- module("Backbone.sync", {setup : function() {
- window.lastRequest = null;
- $.ajax = function(obj) {
- lastRequest = obj;
- };
- }});
+ var ajax = $.ajax
+ var lastRequest = null;
+
+ module("Backbone.sync", {
+
+ setup : function() {
+ $.ajax = function(obj) {
+ lastRequest = obj;
+ };
+ },
+
+ teardown: function() {
+ $.ajax = ajax;
+ }
+
+ });
var Library = Backbone.Collection.extend({
url : function() { return '/library'; }
@@ -20,7 +30,6 @@ $(document).ready(function() {
};
test("sync: read", function() {
- Backbone.sync = originalSync;
library.fetch();
equal(lastRequest.url, '/library');
equal(lastRequest.type, 'GET');
View
1  test/test.html
@@ -21,6 +21,7 @@
<script type="text/javascript" src="view.js"></script>
<script type="text/javascript" src="sync.js"></script>
<script type="text/javascript" src="speed.js"></script>
+ <script type="text/javascript" src="setdomlibrary.js"></script>
</head>
<body>
<h1 id="qunit-header">Backbone Test Suite</h1>
Please sign in to comment.
Something went wrong with that request. Please try again.