backbone-delta
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -1,2 +0,2 @@ | ||
//! backbone-delta.js 0.3.0, https://github.com/Two-Screen/backbone-delta | ||
//! backbone-delta.js 0.3.1, https://github.com/Two-Screen/backbone-delta | ||
//! © 2012 Stéphan Kochen, Angry Bytes. MIT licensed. | ||
@@ -55,10 +55,10 @@ | ||
options = options ? _.clone(options) : {}; | ||
var model = this; | ||
if (options.parse === void 0) options.parse = true; | ||
var success = options.success; | ||
options.success = function(resp, status, xhr) { | ||
if (!model.reset(model.parse(resp, xhr), options)) return false; | ||
options.success = function(model, resp, options) { | ||
var parsed = model.parse(resp, options); | ||
if (!model.reset(parsed, options)) return false; | ||
if (success) success(model, resp); | ||
}; | ||
options.error = Backbone.wrapError(options.error, model, options); | ||
return (this.sync || Backbone.sync).call(this, 'read', this, options); | ||
return this.sync('read', this, options); | ||
}; | ||
@@ -110,11 +110,10 @@ | ||
options = options ? _.clone(options) : {}; | ||
if (options.parse === undefined) options.parse = true; | ||
var collection = this; | ||
if (options.parse === void 0) options.parse = true; | ||
var success = options.success; | ||
options.success = function(resp, status, xhr) { | ||
collection.delta(collection.parse(resp, xhr), options); | ||
if (success) success(collection, resp); | ||
options.success = function(collection, resp, options) { | ||
var parsed = collection.parse(resp, options); | ||
collection.delta(parsed, options); | ||
if (success) success(collection, resp, options); | ||
}; | ||
options.error = Backbone.wrapError(options.error, collection, options); | ||
return (this.sync || Backbone.sync).call(this, 'read', this, options); | ||
return this.sync('read', this, options); | ||
}; | ||
@@ -121,0 +120,0 @@ |
@@ -1,3 +0,3 @@ | ||
//! backbone-delta.js 0.3.0, https://github.com/Two-Screen/backbone-delta | ||
//! backbone-delta.js 0.3.1, https://github.com/Two-Screen/backbone-delta | ||
//! © 2012 Stéphan Kochen, Angry Bytes. MIT licensed. | ||
(function(){var e;typeof require!="undefined"?e=require("underscore"):e=window._;var t=function(t,n){n||(n={});var r=t.Model,s=r.prototype,o=t.Collection,u=o.prototype;s.reset=function(t,n){var i=this.attributes,s={},o;n||(n={});if(!t)return this;t instanceof r&&(t=t.attributes),t=e.clone(t);for(o in i)e.has(t,o)||(t[o]=void 0,s[o]=!0);if(!this.set(t,n))return!1;for(o in s)delete i[o];return this},s.fetchReset=function(n){n=n?e.clone(n):{};var r=this,i=n.success;return n.success=function(e,t,s){if(!r.reset(r.parse(e,s),n))return!1;i&&i(r,e)},n.error=t.wrapError(n.error,r,n),(this.sync||t.sync).call(this,"read",this,n)},u.delta=function(t,n){var r={},s=[],o=[],u,a;n||(n={}),t=e.isArray(t)?t.slice():[t];for(i=0,length=t.length;i<length;i++){a=t[i]=this._prepareModel(t[i],n);if(!a)throw new Error("Can't add an invalid model to a collection");r[a.id]=!0,(this.get(a.id)?s:o).push(a)}return s.length===0?this.reset(t,n):(u=this.select(function(e){return!r[e.id]}),this.remove(u,n),e.each(s,function(e){this.get(e).reset(e,n)},this),this.add(o,n),this)},u.fetchDelta=function(n){n=n?e.clone(n):{},n.parse===undefined&&(n.parse=!0);var r=this,i=n.success;return n.success=function(e,t,s){r.delta(r.parse(e,s),n),i&&i(r,e)},n.error=t.wrapError(n.error,r,n),(this.sync||t.sync).call(this,"read",this,n)};if(!n.noPatch){var a=s.fetch;s.fetch=function(e){return e&&e.reset?this.fetchReset(e):a.call(this,e)};var f=u.fetch;u.fetch=function(e){return e&&e.delta?this.fetchDelta(e):f.call(this,e)}}return t},n=function(e,n){var r=function(){};r.prototype=e;var i=new r;return i.Model=e.Model.extend(),i.Collection=e.Collection.extend({model:i.Model}),t(i,n)},r;typeof require!="undefined"?(r=n(require("backbone")),module.exports=r):(r=n(window.Backbone),window.BBDelta=r),r.extend=t,r.inherit=n})(); | ||
(function(){var _;if(typeof require!=="undefined"){_=require("underscore")}else{_=window._}var extend=function(Backbone,options){options||(options={});var Model=Backbone.Model;var ModelProto=Model.prototype;var Collection=Backbone.Collection;var CollectionProto=Collection.prototype;ModelProto.reset=function(attrs,options){var now=this.attributes,unset={},attr;options||(options={});if(!attrs)return this;if(attrs instanceof Model)attrs=attrs.attributes;attrs=_.clone(attrs);for(attr in now){if(!_.has(attrs,attr)){attrs[attr]=void 0;unset[attr]=true}}if(!this.set(attrs,options))return false;for(attr in unset){delete now[attr]}return this};ModelProto.fetchReset=function(options){options=options?_.clone(options):{};if(options.parse===void 0)options.parse=true;var success=options.success;options.success=function(model,resp,options){var parsed=model.parse(resp,options);if(!model.reset(parsed,options))return false;if(success)success(model,resp)};return this.sync("read",this,options)};CollectionProto.delta=function(models,options){var haveId={},matching=[],added=[],removed,model;options||(options={});models=_.isArray(models)?models.slice():[models];for(i=0,length=models.length;i<length;i++){model=models[i]=this._prepareModel(models[i],options);if(!model){throw new Error("Can't add an invalid model to a collection")}haveId[model.id]=true;(this.get(model.id)?matching:added).push(model)}if(matching.length===0){return this.reset(models,options)}removed=this.select(function(model){return!haveId[model.id]});this.remove(removed,options);_.each(matching,function(model){this.get(model).reset(model,options)},this);this.add(added,options);return this};CollectionProto.fetchDelta=function(options){options=options?_.clone(options):{};if(options.parse===void 0)options.parse=true;var success=options.success;options.success=function(collection,resp,options){var parsed=collection.parse(resp,options);collection.delta(parsed,options);if(success)success(collection,resp,options)};return this.sync("read",this,options)};if(!options.noPatch){var origModelFetch=ModelProto.fetch;ModelProto.fetch=function(options){if(options&&options.reset){return this.fetchReset(options)}else{return origModelFetch.call(this,options)}};var origCollectionFetch=CollectionProto.fetch;CollectionProto.fetch=function(options){if(options&&options.delta){return this.fetchDelta(options)}else{return origCollectionFetch.call(this,options)}}}return Backbone};var inherit=function(Backbone,options){var ctor=function(){};ctor.prototype=Backbone;var sub=new ctor;sub.Model=Backbone.Model.extend();sub.Collection=Backbone.Collection.extend({model:sub.Model});return extend(sub,options)};var BBDelta;if(typeof require!=="undefined"){BBDelta=inherit(require("backbone"));module.exports=BBDelta}else{BBDelta=inherit(window.Backbone);window.BBDelta=BBDelta}BBDelta.extend=extend;BBDelta.inherit=inherit})(); |
@@ -5,3 +5,3 @@ { | ||
"author": "Stéphan Kochen <stephan@kochen.nl>", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"homepage": "https://github.com/Two-Screen/backbone-delta", | ||
@@ -15,3 +15,3 @@ "repository": { | ||
"underscore": "1", | ||
"backbone": "0.9" | ||
"backbone": "0.9.10" | ||
}, | ||
@@ -24,4 +24,4 @@ "devDependencies": { | ||
"test": "tap test.js", | ||
"minify": "uglifyjs -o backbone-delta.min.js backbone-delta.js" | ||
"minify": "uglifyjs --comments '/^!/' -o backbone-delta.min.js backbone-delta.js" | ||
} | ||
} |
@@ -39,3 +39,3 @@ var test = require('tap').test; | ||
model.sync = function(method, model, options) { | ||
options.success({}); | ||
options.success(model, {}, options); | ||
}; | ||
@@ -91,6 +91,6 @@ | ||
collection.sync = function(method, collection, options) { | ||
options.success([ | ||
options.success(collection, [ | ||
{ id: 3 }, | ||
{ id: 7 } | ||
]); | ||
], options); | ||
}; | ||
@@ -97,0 +97,0 @@ |
13055
224
Updatedbackbone@0.9.10