documentcloud/backbone — pull request #2247 by PaulUithol (2013-02-07 14:08:45+00:00)
Title: Update `Backbone.Collection.get` so it can handle edge-cases better.

The changed behavior of `Backbone.Collection.get` has caused some regressions in Backbone-relational, and will probably fail for other people in some cases as well. This is an edge case, but I think `get` should handle it properly. At the very least, it's better to handle it, than not to ;).

Specifically, when a there's already a model in the collection with a `cid`, and `get` is called with a model (or map) containing both an `id` and `cid`, `get` currently won't find the model.

The (very dumbed down) test:

```javascript
test("get with mixed id/cids", 2, function() {
var f = new Backbone.Model({label: 'f'});
col.add(f);
equal(col.get(f), f);
equal(col.get({id:5, cid:f.cid}), f);
});
```
This patch tries to find the model on `cid` or `idAttribute` in case the lookup on `id` fails:

```javascript
// Get a model from the set by id.
get: function(obj) {
if (obj == null) return void 0;
this._idAttr || (this._idAttr = this.model.prototype.idAttribute);
return obj.id && this._byId[obj.id] || obj.cid && this._byId[obj.cid] ||
obj[this._idAttr] && this._byId[obj[this._idAttr]] || this._byId[obj];
},
```

See https://github.com/PaulUithol/Backbone-relational/issues/262