You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP

backbone-rel-partialput

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-rel-partialput - npm Package Compare versions

Comparing version

to
0.11.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){r._resetSyncedAttributes();if(u){u(t)}};var o=n.error;n.error=function(t){if(i.isEqual(r._syncedAttributes,n.partialBaseline)){r._syncedAttributes=l}if(o){o(t)}};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={});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});
//# sourceMappingURL=backbone-partialput-min.js.map

@@ -141,8 +141,9 @@ (function(root, factory) {

var success = options.success;
options.success = function(resp) {
options.success = function(model, resp, options) {
// Reset synced attributes again, so that changes coming from the server are
// considered to be saved.
model._resetSyncedAttributes();
var attrsChangedByBackend = model.parse(resp, options);
_.extend(model._syncedAttributes, attrsChangedByBackend);
if(success) {
success(resp);
success.call(options.context, model, resp, options);
}

@@ -154,3 +155,3 @@ };

var error = options.error;
options.error = function(resp) {
options.error = function(model, resp, options) {
// Only restore original value if no other concurrent save requests

@@ -162,3 +163,3 @@ // have reset the synced attributes in the meantime.

if(error) {
error(resp);
error.call(options.context, model, resp, options);
}

@@ -165,0 +166,0 @@ };

@@ -20,2 +20,3 @@ {

"karma": "~0.12.31",
"karma-cli": "^0.1.1",
"karma-coverage": "~0.2.7",

@@ -40,3 +41,3 @@ "karma-junit-reporter": "~0.2.2",

"main": "backbone-partialput.js",
"version": "0.10.5",
"version": "0.11.0",
"license": "MIT",

@@ -43,0 +44,0 @@ "repository": {

@@ -96,4 +96,59 @@ define(function(require) {

it('should return those attributes that have been changed during the last, now finished save request', function () {
var a = new A({
title: "new_a",
description: "new_desc"
});
a.save();
var attrs = a.unsavedAttributes();
expect(attrs).to.be.empty;
a.set("title", "new_a_changed");
server.requests[0].respond(200, { "Content-Type": "application/json" },
JSON.stringify({})
);
attrs = a.unsavedAttributes();
expect(attrs).to.deep.equal({ title: "new_a_changed" });
});
it('should not return attributes that have been changed by the server', function () {
var a = new A({
title: "new_a",
description: "new_desc"
});
a.save();
server.requests[0].respond(200, { "Content-Type": "application/json" },
JSON.stringify({ title: "new_a_changed" })
);
var attrs = a.unsavedAttributes();
expect(attrs).to.be.empty;
});
});
describe('#unsavedInlinedEmbeddings', function () {
it.skip('should not return inlined embeddings that have been changed by the server', function () {
var b = new B({
e: { name: "emb1" }
});
b.save();
server.requests[0].respond(200,
{ "Content-Type": "application/json" },
JSON.stringify({ e: { name: "emb1_changed" }})
);
expect(b.unsavedInlinedEmbeddings()).to.be.empty;
});
});
describe('#hasUnsavedChanges', function () {
it("should return true if an attribute in the inlined, embedded model has been changed", function() {

@@ -138,2 +193,3 @@ var b = new B({

});

@@ -140,0 +196,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet