Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js: Collection's "change" event isn't firing

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:

  1. Should the above code work as anticipated?
  2. If so, do you have any suggestions on how to track down where this would fail?

Thanks

like image 220
Thomas Avatar asked Nov 17 '11 22:11

Thomas


People also ask

How do I trigger an event in Backbone JS?

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.

How does Backbone js work?

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.

Why use Backbone js?

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.

What is Backbone in Programming?

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).


2 Answers

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"});  
like image 178
Bobby Avatar answered Sep 18 '22 18:09

Bobby


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"}); 
like image 29
timDunham Avatar answered Sep 20 '22 18:09

timDunham