Backbone listenTo()

If you look at the docs (as of today, that is), for the listenTo() method on www.backbonejs.org you'll see that it says it takes three params: the object to listen to, the event to listen for, and the callback to fire when the object triggers the event. But apparently you can also do this, so that you can listen for more than one event on the object:

 

 

view.listenTo(collection,{
'reset': view.render,
'change': view.render
});

 


Consider these variations of listening for events ('this' is a view)

1. this.model.on('change', this.someCallback);

vs.

2. this.listenTo(this.model, 'change', this.someCallback);

In case 1, the view will not get garbage collected because the model has a reference to it.
In case 2, when remove() is called on the view, it will remove the listener on the model so that the view can be freed from memory
BUT don't forget to call remove() on the view.

 

Here are a few links that are related:

Backbone - memory leaks

Backbone - common pitfalls