afeld / backbone-nested

A plugin to make Backbone.js keep track of nested attributes - looking for maintainers! https://github.com/afeld/backbone-nested/issues/157
https://afeld.github.com/backbone-nested/
MIT License
443 stars 83 forks source link

change event not triggered when setting an object that contains an array #131

Open theojapa opened 9 years ago

theojapa commented 9 years ago
var model = new Backbone.NestedModel({
    a: {
        b: [
            { c: 1 },
            { d: 2 }
        ]
    }
});

model.bind('change:a.b[0].c', function() { console.log('c changed.'); });

// The following set triggers the change event:
model.set('a.b[0].c', 3);

// The following does not trigger the change event:
model.set({
    a: {
        b: [
            { c: 4 },
            { d: 2 }
        ]
    }
});

But this works as expected sans array:

var model2 = new Backbone.NestedModel({
    a: {
        b: { 
            c: 1
        }
    }
});

model2.bind('change:a.b.c', function() { console.log('c changed.'); });

// The following set triggers the change event:
model2.set({
    a: {
        b: {
            c: 12
        }
    }
});
afeld commented 9 years ago

Hmm, interesting.

http://jsbin.com/fezuyerire/2/edit?js,console

Seems like a bug, unfortunately :confused: Arrays aren't a problem when they are top-level properties:

http://jsbin.com/fegexe/1/edit?js,console

I can't look into this until this weekend, but would more than welcome a pull request.

theojapa commented 9 years ago

The issue has happens when arrays are top-level properties:

http://jsbin.com/jixepuzace/1/edit?js,console

afeld commented 9 years ago

Ohhhh, gotcha, it's the setting with an array. Bug!

theojapa commented 9 years ago

Have a fix; will create a PR shortly.