documentcloud/backbone — Issue #998: Collection.add event callbacks trigger after all models are added in 0.9.1 instead of serially like 0.5.3
Author: elsigh
Created: 2012-02-13 21:28:11+00:00
State: open

Something in my code broke with the recent update from 0.5.3 -> 0.9.1 and I believe I've traced it down to a difference in how Collection.add works.

In 0.5.3, when the "add" event fired on a collection, the state of the collection was such that the last model in the collection was the one being passed through the add. Put another way, in the add callback on a collection the length was accurate and in sync with the model being added. Does that make sense?

The code was like:
for (var i = 0, l = models.length; i < l; i++) {
this._add(models[i], options);
}

So when the add callback say hits a viewer listening for 'add' the length of the collection would increment on each add callback.

Now, the code is more decoupled and the collection is fully populated when add events are triggered.

splice.apply(this.models, [index, 0].concat(models));
if (this.comparator) this.sort({silent: true});
if (options.silent) return this;
for (i = 0, length = this.models.length; i < length; i++) {
if (!cids[(model = this.models[i]).cid]) continue;
options.index = i;
model.trigger('add', model, this, options);
}

This seems like strange behavior - meaning that add callbacks aren't really callbacks per add but rather a batch after all the models have been added.

Thoughts?