backbone-rel-partialput
Advanced tools
Comparing version 0.11.0 to 0.12.0
@@ -1,2 +0,2 @@ | ||
(function(t,e){if(typeof define==="function"&&define.amd){define(["underscore","backbone-rel","exports"],function(i,s,n){t.Backbone=e(t,n,i,s)})}else if(typeof exports!=="undefined"){var i=require("underscore"),s=require("backbone-rel");e(t,exports,i,s)}else{t.Backbone=e(t,{},t._,t.Backbone)}})(this,function(t,e,i,s){var n=i.extend({},s);var r=function(t,e){var s={};for(var n in e){if(!i.isEqual(t[n],e[n])){s[n]=e[n]}}return s};n.Model=s.Model.extend({partialAttributesCore:["id"],constructor:function(){this._syncedInlinedJSONRepresentation={};return s.Model.prototype.constructor.apply(this,arguments)},unsavedAttributes:function(t){t=t||this.attributes;if(!this._syncedAttributes)return t;return r(this._syncedAttributes,t)},unsavedInlinedEmbeddings:function(t){t=t||{};var e=i.uniq(i.compact(i.flatten(i.union([t.inlineJSON],[i.result(this,"inlineJSON")]))));if(e.length===0)return{};var s=this.toJSON(t);var n={};i.each(e,function(t){var e=t.split(".")[0];if(!i.isEqual(s[e],this._syncedInlinedJSONRepresentation[e])){n[t]=s[e]}},this);return n},hasUnsavedChanges:function(t){t=t||{};return!i.isEmpty(this.unsavedAttributes())||!i.isEmpty(this.unsavedInlinedEmbeddings(t))},discardUnsavedChanges:function(){this.set(this._syncedAttributes,{silent:true});i.each(this.attributes,function(t,e){if(this._syncedAttributes[e]){return}this.unset(e,{silent:true})},this);this.trigger("change")},save:function(t,e,n){var r=this;var a;if(i.isUndefined(t)||i.isNull(t)||typeof t==="object"){a=t;n=e}else{(a={})[t]=e}n||(n={});if(!this.isNew()&&!this.hasUnsavedChanges(n)){if(n.success)n.success({});return}if(n.partial===void 0)n.partial=true;if(n.partial)n.partialBaseline=i.clone(this.attributes);var u=n.success;n.success=function(t,e,s){var n=t.parse(e,s);i.extend(t._syncedAttributes,n);if(u){u.call(s.context,t,e,s)}};var o=n.error;n.error=function(t,e,s){if(i.isEqual(t._syncedAttributes,s.partialBaseline)){t._syncedAttributes=l}if(o){o.call(s.context,t,e,s)}};var c=s.Model.prototype.save.call(this,a,n);var l=this._syncedAttributes;this._resetSyncedAttributes();return c},fetch:function(t){var e=this;t||(t={});var i=t.success;t.success=function(t){e._resetSyncedAttributes();if(i){i(t)}};return s.Model.prototype.fetch.call(this,t)},parse:function(t,e){var i=s.Model.prototype.parse.apply(this,arguments);e||(e={});if(e.partial&&e.partialBaseline){i=r(e.partialBaseline,i);delete e.partialBaseline}return i},toJSON:function(t){var e,n,r;t||(t={});if(t.partial){r=this.get(this.idAttribute||"id");e=this.attributes;this.attributes=i.extend(this.unsavedAttributes(),i.pick(e,this.partialAttributesCore));this.attributes[this.idAttribute||"id"]=r;n=s.Model.prototype.toJSON.apply(this,arguments);this.attributes=e}else{n=s.Model.prototype.toJSON.apply(this,arguments)}return n},set:function(t,e,n){var r,a;if(t===null)return this;if(typeof t==="object"){r=t;n=e}else{(r={})[t]=e}n||(n={});if(n.protectUnsaved){a=i.keys(this.unsavedAttributes());r=i.omit(r,a)}return s.Model.prototype.set.apply(this,[r,n])},_resetSyncedAttributes:function(t){t=t||{};this._syncedAttributes=i.clone(this.attributes);var e=i.uniq(i.compact(i.flatten(i.union([t.inlineJSON],[i.result(this,"inlineJSON")]))));if(e.length>0){this._syncedInlinedJSONRepresentation=this.toJSON(t)}}});i.extend(e,n);return n}); | ||
(function(t,e){if(typeof define==="function"&&define.amd){define(["underscore","backbone-rel","exports"],function(i,s,n){t.Backbone=e(t,n,i,s)})}else if(typeof exports!=="undefined"){var i=require("underscore"),s=require("backbone-rel");e(t,exports,i,s)}else{t.Backbone=e(t,{},t._,t.Backbone)}})(this,function(t,e,i,s){var n=i.extend({},s);var r=function(t,e){var s={};for(var n in e){if(!i.isEqual(t[n],e[n])){s[n]=e[n]}}return s};n.Model=s.Model.extend({partialAttributesCore:["id"],constructor:function(){this._syncedInlinedJSONRepresentation={};return s.Model.prototype.constructor.apply(this,arguments)},unsavedAttributes:function(t){t=t||this.attributes;if(!this._syncedAttributes)return t;return r(this._syncedAttributes,t)},unsavedInlinedEmbeddings:function(t){t=t||{};var e=i.uniq(i.compact(i.flatten(i.union([t.inlineJSON],[i.result(this,"inlineJSON")]))));if(e.length===0)return{};var s=this.toJSON(t);var n={};i.each(e,function(t){var e=t.split(".")[0];if(!i.isEqual(s[e],this._syncedInlinedJSONRepresentation[e])){n[t]=s[e]}},this);return n},hasUnsavedChanges:function(t){t=t||{};return!i.isEmpty(this.unsavedAttributes())||!i.isEmpty(this.unsavedInlinedEmbeddings(t))},discardUnsavedChanges:function(){this.set(this._syncedAttributes,{silent:true});i.each(this.attributes,function(t,e){if(this._syncedAttributes[e]){return}this.unset(e,{silent:true})},this);this.trigger("change")},save:function(t,e,n){var r=this;var a;if(i.isUndefined(t)||i.isNull(t)||typeof t==="object"){a=t;n=e}else{(a={})[t]=e}n||(n={});if(!this.isNew()&&!this.hasUnsavedChanges(n)){if(n.success)n.success({});return}if(n.partial===void 0)n.partial=true;if(n.partial)n.partialBaseline=i.clone(this.attributes);var u=n.success;n.success=function(t,e,s){var n=t.parse(e,s);i.extend(t._syncedAttributes,n);if(u){u.call(s.context,t,e,s)}};var o=n.error;n.error=function(t,e,s){if(i.isEqual(t._syncedAttributes,s.partialBaseline)){t._syncedAttributes=l}if(o){o.call(s.context,t,e,s)}};var c=s.Model.prototype.save.call(this,a,n);var l=this._syncedAttributes;this._resetSyncedAttributes();return c},fetch:function(t){var e=this;t||(t={});t=i.extend({},t,{resetSyncedAttributes:true});return s.Model.prototype.fetch.call(this,t)},parse:function(t,e){var i=s.Model.prototype.parse.apply(this,arguments);e||(e={});if(e.partial&&e.partialBaseline){i=r(e.partialBaseline,i);delete e.partialBaseline}return i},toJSON:function(t){var e,n,r;t||(t={});if(t.partial){r=this.get(this.idAttribute||"id");e=this.attributes;this.attributes=i.extend(this.unsavedAttributes(),i.pick(e,this.partialAttributesCore));this.attributes[this.idAttribute||"id"]=r;n=s.Model.prototype.toJSON.apply(this,arguments);this.attributes=e}else{n=s.Model.prototype.toJSON.apply(this,arguments)}return n},set:function(t,e,n){var r,a;if(t===null)return this;if(typeof t==="object"){r=t;n=e}else{(r={})[t]=e}n||(n={});if(n.protectUnsaved){a=i.keys(this.unsavedAttributes());r=i.omit(r,a)}var u=s.Model.prototype.set.apply(this,[r,n]);if(n.resetSyncedAttributes){this._resetSyncedAttributes()}return u},_resetSyncedAttributes:function(t){t=t||{};this._syncedAttributes=i.clone(this.attributes);var e=i.uniq(i.compact(i.flatten(i.union([t.inlineJSON],[i.result(this,"inlineJSON")]))));if(e.length>0){this._syncedInlinedJSONRepresentation=this.toJSON(t)}}});i.extend(e,n);return n}); | ||
//# sourceMappingURL=backbone-partialput-min.js.map |
@@ -181,16 +181,4 @@ (function(root, factory) { | ||
//if(options.partial === void 0) options.partial = !options.reset; | ||
options = _.extend({}, options, { resetSyncedAttributes: true }); | ||
// This callback will be executed after the attributes from the response | ||
// have been set to the model, before `sync` is triggered | ||
var success = options.success; | ||
options.success = function(resp) { | ||
model._resetSyncedAttributes(); | ||
if(success) { | ||
success(resp); | ||
} | ||
}; | ||
return BackboneBase.Model.prototype.fetch.call(this, options); | ||
@@ -272,3 +260,8 @@ }, | ||
return BackboneBase.Model.prototype.set.apply(this, [attrs, options]); | ||
var result = BackboneBase.Model.prototype.set.apply(this, [attrs, options]); | ||
if(options.resetSyncedAttributes) { | ||
this._resetSyncedAttributes(); | ||
} | ||
return result; | ||
}, | ||
@@ -275,0 +268,0 @@ |
@@ -40,3 +40,3 @@ { | ||
"main": "backbone-partialput.js", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"license": "MIT", | ||
@@ -43,0 +43,0 @@ "repository": { |
@@ -17,3 +17,3 @@ define(function(require) { | ||
e: Backbone.Model, | ||
c: Backbone.Collection | ||
c: Backbone.Collection.extend({ model: Backbone.Model }) | ||
}, | ||
@@ -27,2 +27,4 @@ | ||
autoFetchRelated: false, | ||
url: function () { return "a/" + this.id } | ||
@@ -194,3 +196,29 @@ | ||
it('should return false for embedded objects which have been set through inlined data in the fetch response from its parent', function () { | ||
var b = new B({ | ||
id: "b1" | ||
}); | ||
b.fetch(); | ||
server.requests[0].respond(200, | ||
{ "Content-Type": "application/json" }, | ||
JSON.stringify({ id: "b1", e: { name: "emb1" } }) | ||
); | ||
expect(b.get("e").hasUnsavedChanges()).to.be.false; | ||
}); | ||
it('should return false for embedded objects which have been set through inlined collection data in the fetch response from its parent', function () { | ||
var b = new B({ | ||
id: "b2" | ||
}); | ||
b.fetch(); | ||
server.requests[0].respond(200, | ||
{ "Content-Type": "application/json" }, | ||
JSON.stringify({ id: "b1", c: [ { id: "c1", title: "item1" } ] }) | ||
); | ||
expect(b.get("c").models[0].hasUnsavedChanges()).to.be.false; | ||
}); | ||
}); | ||
@@ -197,0 +225,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
437722
1445