documentcloud/backbone — pull request #1752 by abernier (2012-10-18 16:16:56+00:00)
Title: {merge: true} option for Collection::reset

A great addition to Backbone `>0.9.2` (actually `master`) is the `merge` option for `Collection::add` :

```javascript
Dog = Backbone.Model.extend({});
Dogs = Backbone.Collection.extend({model: Dog});

myDogs = new Dogs();
myDogs.add({id: 1, name: 'rex'}); // 'add'
myDogs.add({id: 1, name: 'Rex'}); // 'change'
```

--

This PR takes advantage on this new feature and applies it to `Collection::reset` in order to enable triggering individual `'add'`/`'change'`, `remove` events when `reset`ting a collection.

Let's have an example:

```javascript
// As usual:
myDogs = new Dogs();
myDogs.reset([
{id: 1, name: 'rex'},
{id: 2, name: 'bill'}
]); // 'reset'

// And now with merge option:
myDogs.reset([
{id: 1, name: 'Rex'}, // rex has changed to Rex => 'change'
// bill has gone => 'remove'
{id: 3, name: 'Snowy'} // Snowy is coming => 'add'
], {merge: true});
```

NB : with `{merge: true}`, `'reset'` event is no longer triggered (like `'add'` is not for `Collection::add` with the same option).

If you want no event at all, just pass `{silent: true}` option.

This can be used in conjunction with `Collection::fetch` by passing `{merge: true}` option to it and stay compatible with the `{add: true}` option.

--

It comes with 24 tests under "reset and merge".

I'm quite new to Backbone so please review my implementation and tell me your opinion about it.

Cheers