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

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

Open
theojapa opened this issue Mar 5, 2015 · 4 comments
Open

Comments

@theojapa
Copy link

theojapa commented Mar 5, 2015

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
Copy link
Owner

afeld commented Mar 5, 2015

Hmm, interesting.

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

Seems like a bug, unfortunately 😕 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
Copy link
Author

theojapa commented Mar 5, 2015

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

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

@afeld
Copy link
Owner

afeld commented Mar 5, 2015

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

@theojapa
Copy link
Author

theojapa commented Mar 5, 2015

Have a fix; will create a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants