Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#146 Changing nested array variables does not set 'changedAttributes' #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]');
});