documentcloud/backbone — Issue #689: Collection Sort on Model Change
Author: snoe
Created: 2011-10-25 23:23:16+00:00
State: open

The documentation for Collection.sort says
> a collection with a comparator function will maintain itself in proper sort order at all times.

Yet, if my comparator is

comparator: function(model) {
return model.get("name");
}

Then if I change the name:

model.set({name: 'zzz'});

and in the change event I use indexOf, I get the wrong index:

model.bind('change', changed);
changed = function(model) {
var index = model.collection.indexOf(model)
// index is based off the old name
}

This is because the collection does not resort automatically on change. I think the documentation should change, or preferably, a this.sort({silent}) in Collection._onModelEvent should occur.