Skip to content
Closed
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: 5 additions & 1 deletion backbone-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@
var nestedAttr, nestedVal;
for (var a in obj){
if (obj.hasOwnProperty(a)) {
nestedAttr = prefix + '.' + a;
if (_.isArray(obj)) {
nestedAttr = prefix + '[' + a + ']';
} else {
nestedAttr = prefix + '.' + a;
}
nestedVal = obj[a];
if (!_.isEqual(model.get(nestedAttr), nestedVal)) {
model._delayedChange(nestedAttr, nestedVal, opts);
Expand Down
28 changes: 28 additions & 0 deletions test/nested-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,34 @@ $(document).ready(function() {
sinon.assert.calledOnce(changeGender);
});

test("change event on top-level attribute with array", function() {
var change = sinon.spy();
var changeAddresses = sinon.spy();
var changeAddresses0 = sinon.spy();
var changeAddresses0City = sinon.spy();
var changeAddresses0State = sinon.spy();

doc.bind('change', change);
doc.bind('change:addresses', changeAddresses);
doc.bind('change:addresses[0]', changeAddresses0);
doc.bind('change:addresses[0].city', changeAddresses0City);
doc.bind('change:addresses[0].state', changeAddresses0State);

doc.set({'addresses': [
{
city: 'Seattle',
state: 'WA'
}
]});

// Confirm all triggers fire once that should
sinon.assert.calledOnce(change);
sinon.assert.calledOnce(changeAddresses);
sinon.assert.calledOnce(changeAddresses0);
sinon.assert.calledOnce(changeAddresses0City);
sinon.assert.calledOnce(changeAddresses0State);
});

test("change event should fire after attribute is set", function() {
var callback = function(model){
equal(model.get('name.first'), 'Bob');
Expand Down