documentcloud/backbone — pull request #1873 by caseywebdev (2012-12-05 19:17:20+00:00)
Title: Collection#update to reduce waste

I created a PR a few days ago about this and (A) the PR became out of sync with my repo and (B) I feel I explained my rationale poorly which lead to its abrupt close. So I'm sorry for the poor re-issue etiquette, but I really think this should be at least discussed.

`reset` definitely has it's place as a quick way to completely wipe a collection and start over with new models. Often times though, I'd like to update a collection without obliterating my current models but rather firing off helpful `add`/`change`/`remove` events. This allows me to poll the server with `fetch` and effectively listen for these events without fear of having to recreate any models (wasteful). Here is an example:

```js
var UsersView = Backbone.View.extend({
delegateEvents: function () {
Backbone.View.prototype.delegateEvents.apply(this, arguments);
this.collection.on('add', this.addUser, this);
this.pollInterval = setInterval(_.bind(this.poll, this), 10000);
},

undelegateEvents: function () {
clearInterval(this.pollInterval);
Backbone.View.prototype.delegateEvents.apply(this, arguments);
},

addUser: function (user) {
var userView = new UserView({model: user});
this.$el.append((userView).el);
},

poll: function () {
// Ideally `update` is the default action, but that would be a
// breaking change so the options object is used for now.
this.collection.fetch({update: true});
}
});

var UserView = Backbone.View.extend({
initialize: function() {
// Can be fine tuned thanks to `change:attr`, but in general...
this.model.on('change', this.render, this);
this.model.on('remove', this.remove, this);
}
});
```

This is not a complete example, but you can see what I'm getting at. The ability to rig a view (and subviews) this way makes for a clean, clear intent as to what should happen when an `add`/`change`/`remove` happens. The only alternative right now is a not-so-pretty `reset` dance that just doesn't use Backbone.Events to their potential.

Again, I apologize for the duplicate but I would really appreciate some input especially before the release is cut.