Tweak add implementation.

This commit is contained in:
Brad Dunbar
2012-10-08 09:31:11 -04:00
parent c2261bf962
commit 9b6826929c
2 changed files with 26 additions and 28 deletions

View File

@@ -597,19 +597,19 @@
// Add a model, or list of models to the set. Pass **silent** to avoid
// firing the `add` event for every new model.
add: function(models, options) {
var i, args, length, model, existing, valid;
var i, args, length, model, existing;
var at = options && options.at;
models = _.isArray(models) ? models.slice() : [models];
// Begin by turning bare objects into model references, and preventing
// invalid models from being added.
valid = [];
for (i = 0, length = models.length; i < length; i++) {
model = this._prepareModel(models[i], options);
if(!model) {
this.trigger("error", this, "Can't add an invalid model to a collection", models[i]);
for (i = models.length - 1; i >= 0; i--) {
// Turn bare objects into model references, and prevent invalid models
// from being added.
if(!(model = this._prepareModel(models[i], options))) {
this.trigger("error", this, models[i], options);
models.splice(i, 1);
continue;
}
models[i] = model;
existing = model.id != null && this._byId[model.id];
// If a duplicate is found, prevent it from being added and
@@ -618,10 +618,10 @@
if (options && options.merge && existing) {
existing.set(model, options);
}
models.splice(i, 1);
continue;
}
valid.push(model);
// Listen to added models' events, and index models for lookup by
// `id` and by `cid`.
model.on('all', this._onModelEvent, this);
@@ -630,9 +630,9 @@
}
// Update `length` and splice in new models.
this.length += valid.length;
this.length += models.length;
args = [at != null ? at : this.models.length, 0];
push.apply(args, valid);
push.apply(args, models);
splice.apply(this.models, args);
// Sort the collection if appropriate.
@@ -641,7 +641,7 @@
if (options && options.silent) return this;
// Trigger `add` events.
while (model = valid.shift()) {
while (model = models.shift()) {
model.trigger('add', model, this, options);
}

View File

@@ -543,7 +543,6 @@ $(document).ready(function() {
});
test("#861, adding models to a collection which do not pass validation", 2, function() {
var fired = null;
var Model = Backbone.Model.extend({
validate: function(attrs) {
if (attrs.id == 3) return "id can't be 3";
@@ -554,27 +553,26 @@ $(document).ready(function() {
model: Model
});
var col = new Collection;
col.on("error", function() { fired = true; });
var collection = new Collection;
collection.on("error", function() { ok(true); });
col.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
equal(fired, true);
equal(col.length, 5);
collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
});
test("invalid models are discarded and only valid models are added to a collection", 5, function() {
var col = new Backbone.Collection();
col.on('test', function() { ok(true); });
col.model = Backbone.Model.extend({
test("Invalid models are discarded.", 5, function() {
var collection = new Backbone.Collection;
collection.on('test', function() { ok(true); });
collection.model = Backbone.Model.extend({
validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
});
var model = new col.model({id: 1, valid: true});
col.add([model, {id: 2}]);;
var model = new collection.model({id: 1, valid: true});
collection.add([model, {id: 2}]);;
model.trigger('test');
ok(col.getByCid(model.cid));
ok(col.get(1));
ok(!col.get(2));
equal(col.length, 1);
ok(collection.getByCid(model.cid));
ok(collection.get(1));
ok(!collection.get(2));
equal(collection.length, 1);
});
test("multiple copies of the same model", 3, function() {