Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 13, 2019
2 parents 75e6d0c + b3a2813 commit 0b1c2e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
16 changes: 8 additions & 8 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@
get: function(obj) {
if (obj == null) return void 0;
return this._byId[obj] ||
this._byId[this.modelId(this._isModel(obj) ? obj.attributes : obj)] ||
this._byId[this.modelId(this._isModel(obj) ? obj.attributes : obj, obj.idAttribute)] ||
obj.cid && this._byId[obj.cid];
},

Expand Down Expand Up @@ -1098,8 +1098,8 @@
},

// Define how to uniquely identify models in the collection.
modelId: function(attrs) {
return attrs[this.model.prototype.idAttribute || 'id'];
modelId: function(attrs, idAttribute) {
return attrs[idAttribute || this.model.prototype.idAttribute || 'id'];
},

// Get an iterator of all models in this collection.
Expand Down Expand Up @@ -1154,7 +1154,7 @@
// Remove references before triggering 'remove' event to prevent an
// infinite loop. #3693
delete this._byId[model.cid];
var id = this.modelId(model.attributes);
var id = this.modelId(model.attributes, model.idAttribute);
if (id != null) delete this._byId[id];

if (!options.silent) {
Expand All @@ -1177,15 +1177,15 @@
// Internal method to create a model's ties to a collection.
_addReference: function(model, options) {
this._byId[model.cid] = model;
var id = this.modelId(model.attributes);
var id = this.modelId(model.attributes, model.idAttribute);
if (id != null) this._byId[id] = model;
model.on('all', this._onModelEvent, this);
},

// Internal method to sever a model's ties to a collection.
_removeReference: function(model, options) {
delete this._byId[model.cid];
var id = this.modelId(model.attributes);
var id = this.modelId(model.attributes, model.idAttribute);
if (id != null) delete this._byId[id];
if (this === model.collection) delete model.collection;
model.off('all', this._onModelEvent, this);
Expand All @@ -1200,8 +1200,8 @@
if ((event === 'add' || event === 'remove') && collection !== this) return;
if (event === 'destroy') this.remove(model, options);
if (event === 'change') {
var prevId = this.modelId(model.previousAttributes());
var id = this.modelId(model.attributes);
var prevId = this.modelId(model.previousAttributes(), model.idAttribute);
var id = this.modelId(model.attributes, model.idAttribute);
if (prevId !== id) {
if (prevId != null) delete this._byId[prevId];
if (id != null) this._byId[id] = model;
Expand Down
33 changes: 21 additions & 12 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@
.filter(function(o){ return o.id % 2 === 0; })
.map(function(o){ return o.id * 2; })
.value(),
[4, 0]);
[4, 0]);
assert.deepEqual(col.difference([c, d]), [a, b]);
assert.ok(col.includes(col.sample()));

Expand Down Expand Up @@ -794,8 +794,8 @@
assert.deepEqual(coll.map('a'), [1, 2, 3, 4]);
assert.deepEqual(coll.sortBy('a')[3], model);
assert.deepEqual(coll.sortBy('e')[0], model);
assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1});
assert.deepEqual(coll.countBy('d'), {'undefined': 4});
assert.deepEqual(coll.countBy({a: 4}), {false: 3, true: 1});
assert.deepEqual(coll.countBy('d'), {undefined: 4});
assert.equal(coll.findIndex({b: 1}), 0);
assert.equal(coll.findIndex({b: 9}), -1);
assert.equal(coll.findLastIndex({b: 1}), 3);
Expand Down Expand Up @@ -1478,7 +1478,7 @@
assert.expect(3);
var collection = new (Backbone.Collection.extend({
comparator: function(m1, m2) {
return m1.get('a') > m2.get('a') ? 1 : (m1.get('a') < m2.get('a') ? -1 : 0);
return m1.get('a') > m2.get('a') ? 1 : m1.get('a') < m2.get('a') ? -1 : 0;
}
}))([{id: 1}, {id: 2}, {id: 3}]);
collection.on('sort', function() { assert.ok(true); });
Expand Down Expand Up @@ -1688,12 +1688,20 @@

QUnit.test('modelId', function(assert) {
var Stooge = Backbone.Model.extend();
var StoogeCollection = Backbone.Collection.extend({model: Stooge});
var StoogeCollection = Backbone.Collection.extend();

// Default to using `Collection::model::idAttribute`.
// Default to using `id` if `model::idAttribute` and `Collection::model::idAttribute` not present.
assert.equal(StoogeCollection.prototype.modelId({id: 1}), 1);

// Default to using `model::idAttribute` if present.
Stooge.prototype.idAttribute = '_id';
var model = new Stooge({_id: 1});
assert.equal(StoogeCollection.prototype.modelId(model.attributes, model.idAttribute), 1);

// Default to using `Collection::model::idAttribute` if model::idAttribute not present.
StoogeCollection.prototype.model = Stooge;
assert.equal(StoogeCollection.prototype.modelId({_id: 1}), 1);

});

QUnit.test('Polymorphic models work with "simple" constructors', function(assert) {
Expand Down Expand Up @@ -1748,7 +1756,7 @@
assert.equal(collection.at(1), collection.get('b-1'));
});

QUnit.test('Collection with polymorphic models receives default id from modelId', function(assert) {
QUnit.test('Collection with polymorphic models receives id from modelId using model instance idAttribute', function(assert) {
assert.expect(6);
// When the polymorphic models use 'id' for the idAttribute, all is fine.
var C1 = Backbone.Collection.extend({
Expand All @@ -1761,7 +1769,8 @@
assert.equal(c1.modelId({id: 1}), 1);

// If the polymorphic models define their own idAttribute,
// the modelId method should be overridden, for the reason below.
// the modelId method will use the model's idAttribute property before the
// collection's model constructor's.
var M = Backbone.Model.extend({
idAttribute: '_id'
});
Expand All @@ -1771,12 +1780,12 @@
}
});
var c2 = new C2({_id: 1});
assert.equal(c2.get(1), void 0);
assert.equal(c2.modelId(c2.at(0).attributes), void 0);
assert.equal(c2.get(1), c2.at(0));
assert.equal(c2.modelId(c2.at(0).attributes, c2.at(0).idAttribute), 1);
var m = new M({_id: 2});
c2.add(m);
assert.equal(c2.get(2), void 0);
assert.equal(c2.modelId(m.attributes), void 0);
assert.equal(c2.get(2), m);
assert.equal(c2.modelId(m.attributes, m.idAttribute), 2);
});

QUnit.test('Collection implements Iterable, values is default iterator function', function(assert) {
Expand Down

0 comments on commit 0b1c2e3

Please sign in to comment.