Skip to content

Commit

Permalink
afeld#146 Changing nested array variables does not set 'changedAttrib…
Browse files Browse the repository at this point in the history
…utes'
  • Loading branch information
z0091 committed Feb 13, 2018
1 parent 7d69a11 commit 1c98427
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions backbone-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions test/nested-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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]');
});

0 comments on commit 1c98427

Please sign in to comment.