documentcloud/backbone — pull request #2189 by pixelhandler (2013-01-25 08:23:20+00:00)
Title: collection.get(model) uses _byId[model.id] however id may be added after...

... model added, so fallback to get with cid gracefully

Use case:

A model (without an id) is created and added to a collection. Then the model is assigned an id property, perhaps by a custom sync such as localStorage persistence. The collection's get() method should use the id or the cid to get the model. Internally the collection sets a reference on a property `_byId`, by `id` if set and by cid; however, when get() is called the `_byId` may not have a reference to the new `id` property.

So collection.get() should use the model.id or fallback gracefully to the model.cid property to get the model.

Unit test added "add, assign id to model, then get" and minor change made to get():

```
get: function(obj) {
if (obj == null) return void 0;
this._idAttr || (this._idAttr = this.model.prototype.idAttribute);
- return this._byId[obj.id || obj.cid || obj[this._idAttr] || obj];
+ return this._byId[obj.id] || this._byId[obj.cid || obj[this._idAttr] || obj];
},
```