Skip to content

Commit

Permalink
Revert "Make new tests more thorough"
Browse files Browse the repository at this point in the history
This reverts commit f814b85.

Revert "Return `id` assumption for simple polymorphic `Collection#model` factories."

This reverts commit 3f7fbde.

Conflicts:
	test/collection.js

Revert "jashkenas#2985 -- use 'change-id' as the event name"

This reverts commit 36e4925.

Revert "Merge pull request jashkenas#2985 from caseywebdev/generate-id"

This reverts commit 60b9cc1, reversing
changes made to 0c1838b.

Conflicts:
	test/collection.js
  • Loading branch information
caseywebdev committed Apr 23, 2014
1 parent 17b6976 commit 11cc0e8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 93 deletions.
24 changes: 7 additions & 17 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,6 @@
// CouchDB users may want to set this to `"_id"`.
idAttribute: 'id',

// The function that will generate an id for a model given that model's
// attributes.
generateId: function (attrs) {
return attrs[this.idAttribute];
},

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
Expand Down Expand Up @@ -358,6 +352,9 @@
}
current = this.attributes, prev = this._previousAttributes;

// Check for changes of `id`.
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];

// For each `set` attribute, update or delete the current value.
for (attr in attrs) {
val = attrs[attr];
Expand All @@ -370,10 +367,6 @@
unset ? delete current[attr] : current[attr] = val;
}

var prevId = this.id;
this.id = this.generateId(current);
if (prevId !== this.id) this.trigger('change-id', this, prevId, options);

// Trigger all relevant attribute changes.
if (!silent) {
if (changes.length) this._pending = options;
Expand Down Expand Up @@ -576,7 +569,7 @@

// A model is new if it has never been saved to the server, and lacks an id.
isNew: function() {
return this.id == null;
return !this.has(this.idAttribute);
},

// Check if the model is currently in a valid state.
Expand Down Expand Up @@ -701,18 +694,15 @@
var toAdd = [], toRemove = [], modelMap = {};
var add = options.add, merge = options.merge, remove = options.remove;
var order = !sortable && add && remove ? [] : false;
var targetProto = this.model.prototype;

// Turn bare objects into model references, and prevent invalid models
// from being added.
for (var i = 0, length = models.length; i < length; i++) {
attrs = models[i] || {};
if (this._isModel(attrs)) {
id = model = attrs;
} else if (targetProto.generateId) {
id = targetProto.generateId(attrs);
} else {
id = attrs[targetProto.idAttribute || Model.prototype.idAttribute];
id = attrs[this.model.prototype.idAttribute || 'id'];
}

// If a duplicate is found, prevent it from being added and
Expand Down Expand Up @@ -977,8 +967,8 @@
_onModelEvent: function(event, model, collection, options) {
if ((event === 'add' || event === 'remove') && collection !== this) return;
if (event === 'destroy') this.remove(model, options);
if (event === 'change-id') {
if (collection != null) delete this._byId[collection];
if (model && event === 'change:' + model.idAttribute) {
delete this._byId[model.previous(model.idAttribute)];
if (model.id != null) this._byId[model.id] = model;
}
this.trigger.apply(this, arguments);
Expand Down
61 changes: 0 additions & 61 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1356,67 +1356,6 @@
strictEqual(collection.models.length, 0);
});

test("Models shouldn't be lost by set({id: 1}, {silent: true})", function () {
var collection = new Backbone.Collection([{name: 'Curly'}]);
collection.first().set({id: 1}, {silent: true});
equal(collection.get(1), collection.first());
});

test('Polymorphic models work with "simple" constructors', function () {
var A = Backbone.Model.extend();
var B = Backbone.Model.extend();
var C = Backbone.Collection.extend({
model: function (attrs) {
return attrs.type === 'a' ? new A(attrs) : new B(attrs);
}
});
var collection = new C([{id: 1, type: 'a'}, {id: 2, type: 'b'}]);
equal(collection.length, 2);
ok(collection.at(0) instanceof A);
equal(collection.at(0).id, 1);
ok(collection.at(1) instanceof B);
equal(collection.at(1).id, 2);
});

test('Polymorphic models work with "advanced" constructors', function () {
var A = Backbone.Model.extend({idAttribute: '_id'});
var B = Backbone.Model.extend({idAttribute: '_id'});
var C = Backbone.Collection.extend({
model: Backbone.Model.extend({
constructor: function (attrs) {
return attrs.type === 'a' ? new A(attrs) : new B(attrs);
},

idAttribute: '_id'
})
});
var collection = new C([{_id: 1, type: 'a'}, {_id: 2, type: 'b'}]);
equal(collection.length, 2);
ok(collection.at(0) instanceof A);
equal(collection.at(0).id, 1);
ok(collection.at(1) instanceof B);
equal(collection.at(1).id, 2);

A.prototype.generateId = B.prototype.generateId = function (attrs) {
return attrs.type + '-' + attrs.id;
}
C = Backbone.Collection.extend({
model: Backbone.Model.extend({
constructor: function (attrs) {
return attrs.type === 'a' ? new A(attrs) : new B(attrs);
},

generateId: A.prototype.generateId
})
});
collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]);
equal(collection.length, 2);
ok(collection.at(0) instanceof A);
equal(collection.at(0).id, 'a-1');
ok(collection.at(1) instanceof B);
equal(collection.at(1).id, 'b-1');
});

test("create with wait, model instance, #3028", 1, function() {
var collection = new Backbone.Collection();
var model = new Backbone.Model({id: 1});
Expand Down
15 changes: 0 additions & 15 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,19 +1127,4 @@
model.set({a: true});
});

test("generateId", function() {
var Model = Backbone.Model.extend();

// Simple default uses `idAttribute`
equal(Model.prototype.generateId({id: 1}), 1);
Model.prototype.idAttribute = '_id';
equal(Model.prototype.generateId({_id: 1}), 1);

// Composite key example
Model.prototype.generateId = function (attrs) {
return attrs.a + '-' + attrs.b;
};
equal((new Model({a: 123, b: 456})).id, '123-456');
});

})();

0 comments on commit 11cc0e8

Please sign in to comment.