I have a pretty simple collection, but I can’t seem to bind to it’s change event. In Chrome’s console, I’m running:
var c = new AwesomeCollection();
c.bind("change", function(){
console.log('Collection has changed.');
});
c.add({testModel: "Test"}); // Shouldn't this trigger the above log statement?
Since this is one of those things that can be difficult to track down, I doubt anybody knows off the top of their head what’s going on (if so, great!). So, I’m asking two questions:
- Should the above code work as anticipated?
- If so, do you have any suggestions on how to track down where this would fail?
Thanks
The
changeevent is only fired when one of the collections’ models are modified. When a model is added to the collection theaddevent is fired.See Backbone.js’ Collection Documentation:
To listen for when an
addoccurs modify your code to be