From 65ec60ed127335ce8c9202d9778d191ba466dfb3 Mon Sep 17 00:00:00 2001 From: Johannes Date: Mon, 17 Dec 2012 17:00:12 +0000 Subject: [PATCH 1/3] Improve getting of models with non-default ids Just as you can get a model from a collection using default ids by passing .get() an object i.e. col.get({id: 1}), you can now do the same when using non-default ids i.e. col.get({_id: 1}) (If the collection prototype has the correct model set). --- backbone.js | 3 ++- test/collection.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/backbone.js b/backbone.js index 3e2963ea..b5a1da43 100644 --- a/backbone.js +++ b/backbone.js @@ -734,7 +734,8 @@ // Get a model from the set by id. get: function(obj) { if (obj == null) return void 0; - return this._byId[obj.id != null ? obj.id : obj] || this._byCid[obj.cid || obj]; + var idAttr = this.model.prototype.idAttribute; + return this._byId[obj[idAttr] != null ? obj[idAttr] : obj] || this._byCid[obj.cid || obj]; }, // Get the model at the given index. diff --git a/test/collection.js b/test/collection.js index 4c79e290..0a553b36 100644 --- a/test/collection.js +++ b/test/collection.js @@ -62,13 +62,14 @@ $(document).ready(function() { strictEqual(collection.last().get('a'), 4); }); - test("get", 3, function() { + test("get", 4, function() { equal(col.get(0), d); equal(col.get(2), b); + equal(col.get({id: 1}), c); equal(col.get(col.first().cid), col.first()); }); - test("get with non-default ids", 2, function() { + test("get with non-default ids", 3, function() { var col = new Backbone.Collection(); var MongoModel = Backbone.Model.extend({ idAttribute: '_id' @@ -78,6 +79,11 @@ $(document).ready(function() { equal(col.get(100), model); model.set({_id: 101}); equal(col.get(101), model); + + var Col2 = Backbone.Collection.extend({ model: MongoModel }); + col2 = new Col2(); + col2.push(model); + equal(col2.get({_id: 101}), model); }); test("update index when id changes", 3, function() { From 9ae12715e2c6b0925e43fd742797326d9a465942 Mon Sep 17 00:00:00 2001 From: Johannes Date: Mon, 17 Dec 2012 17:48:56 +0000 Subject: [PATCH 2/3] Make use of new .get() functionality in .update() --- backbone.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backbone.js b/backbone.js index b5a1da43..6b5cf893 100644 --- a/backbone.js +++ b/backbone.js @@ -782,7 +782,6 @@ update: function(models, options) { var model, i, l, existing; var add = [], remove = [], modelMap = {}; - var idAttr = this.model.prototype.idAttribute; options = _.extend({add: true, merge: true, remove: true}, options); if (options.parse) models = this.parse(models, options); @@ -795,7 +794,7 @@ // Determine which models to add and merge, and which to remove. for (i = 0, l = models.length; i < l; i++) { model = models[i]; - existing = this.get(model.id || model.cid || model[idAttr]); + existing = this.get(model); if (options.remove && existing) modelMap[existing.cid] = true; if ((options.add && !existing) || (options.merge && existing)) { add.push(model); From 17fba9e8bd1afa02e4cefa1c6f489cb69c1ea415 Mon Sep 17 00:00:00 2001 From: Johannes Date: Mon, 17 Dec 2012 19:21:34 +0000 Subject: [PATCH 3/3] Fix getting model from collection by model Previously wouldn't work if using non-default id. Thanks to @caseywebdev for pointing this out. --- backbone.js | 3 ++- test/collection.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/backbone.js b/backbone.js index 6b5cf893..e70fe489 100644 --- a/backbone.js +++ b/backbone.js @@ -735,7 +735,8 @@ get: function(obj) { if (obj == null) return void 0; var idAttr = this.model.prototype.idAttribute; - return this._byId[obj[idAttr] != null ? obj[idAttr] : obj] || this._byCid[obj.cid || obj]; + return this._byId[obj[idAttr] != null ? obj[idAttr] : obj.id != null ? obj.id : obj] + || this._byCid[obj.cid || obj]; }, // Get the model at the given index. diff --git a/test/collection.js b/test/collection.js index 0a553b36..9ac954ed 100644 --- a/test/collection.js +++ b/test/collection.js @@ -62,14 +62,15 @@ $(document).ready(function() { strictEqual(collection.last().get('a'), 4); }); - test("get", 4, function() { + test("get", 5, function() { equal(col.get(0), d); equal(col.get(2), b); equal(col.get({id: 1}), c); + equal(col.get(c.clone()), c); equal(col.get(col.first().cid), col.first()); }); - test("get with non-default ids", 3, function() { + test("get with non-default ids", 4, function() { var col = new Backbone.Collection(); var MongoModel = Backbone.Model.extend({ idAttribute: '_id' @@ -84,6 +85,7 @@ $(document).ready(function() { col2 = new Col2(); col2.push(model); equal(col2.get({_id: 101}), model); + equal(col2.get(model.clone()), model); }); test("update index when id changes", 3, function() {