documentcloud/backbone — pull request #1018 by proof (2012-02-17 00:22:19+00:00)
Title: View events

This allows backbone to automatically bind/unbind events to a collection or model referenced by that view:

var ViewClass = Backbone.View.extend({
events: {
'collection reset': 'collectionReset',
'model change': 'modelChange'
},

collectionReset: function() {
console.log('Collection has been reset.');
},

modelChange: function() {
console.log('Model has been changed.');
}
});

// you also don't have to worry about manually unhooking events
var view = new ViewClass({ model: model, collection: collection });

// ... do something

When you are finished with the view, detach events like this:

view.undelegateEvents();

Good idea? It saves a lot of time, although maybe the code for detaching events could be in a separate method which also destroys the DOM element...