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:
Thanks
js trigger Event is used to invoke or callback the function for the given event or a space-delimited list of events. The subsequent arguments will be passed along to the event callbacks in order to trigger it. Parameter Values: event: It is used to bind an object with an event.
Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.
Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).
The change
event is only fired when one of the collections' models are modified. When a model is added to the collection the add
event is fired.
See Backbone.js' Collection Documentation:
You can to bind "change" events to be notified when any model in the collection has been modified, listen for "add" and "remove" events[...]
To listen for when an add
occurs modify your code to be
var c = new AwesomeCollection(); c.bind("add", function(){ console.log('Collection has changed.'); }); c.add({testModel: "Test"});
No, that only raises the "add" event. It will raise the change event if you do this:
var c = new AwesomeCollection(); c.bind("change", function() { console.log('Collection has changed.'); }); var model = new Backbone.Model({testModel: "Test"}); c.add(model); model.set({testModel: "ChangedTest"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With