From 1c984274d6ada2bf620ad05e1932bfc54a089141 Mon Sep 17 00:00:00 2001 From: Alexander Plotnikov Date: Tue, 13 Feb 2018 20:44:43 +0400 Subject: [PATCH] #146 Changing nested array variables does not set 'changedAttributes' --- backbone-nested.js | 6 ++++-- test/nested-model.js | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/backbone-nested.js b/backbone-nested.js index 648f009..9b74a2d 100644 --- a/backbone-nested.js +++ b/backbone-nested.js @@ -176,10 +176,12 @@ var trigger = !opts.silent && (val.length >= i + 1), oldEl = val[i]; + // we should not change the original value + var aryVal = Backbone.NestedModel.deepClone(val); // remove the element from the array - val.splice(i, 1); + aryVal.splice(i, 1); opts.silent = true; // Triggers should only be fired in trigger section below - this.set(aryPath, val, opts); + this.set(aryPath, aryVal, opts); if (trigger){ attrStr = Backbone.NestedModel.createAttrStr(aryPath); diff --git a/test/nested-model.js b/test/nested-model.js index bc1a31a..3bda4d2 100644 --- a/test/nested-model.js +++ b/test/nested-model.js @@ -861,6 +861,21 @@ test("#changedAttributes() should clear the nested attributes between change eve doc.set({'name.last': 'Dylan'}, {validate: true}); }); +test("#changedAttributes() should clear the nested attributes between change events", function() { + doc.bind('change', function(){ + deepEqual(this.changedAttributes(), { + addresses: [ + { + city: 'Brooklyn', + state: 'NY' + } + ] + }); + }); + + doc.remove('addresses[1]'); +}); + // ----- CLEAR -------- test("#clear()", function() { @@ -1146,4 +1161,38 @@ test("issue #68 - _delayedTriggers is a singleton", function() { modelA.set("name.first", "s"); }); +test("issue #146 - Changing nested array variables does not set 'changedAttributes'", function() { + var model = new Klass({ + structure: { + abc: [ + { + items: [1, 2, 3], + groupId: 123 + }, + { + items: [4 ,5, 6], + groupId: 456 + } + ] + } + }); + model.bind('change', function () { + deepEqual(this.changedAttributes(), { + structure: { + abc: [ + { + items: [1, 2, 3], + groupId: 123 + }, + { + items: [4 ,5], + groupId: 456 + } + ] + } + }); + }); + + model.remove('structure.abc[1].items[2]'); +});