I am setting and unsetting comparator functions on my backbone collections at runtime, and I would like to know if there is a way to "reset" the collection to its original insertion order. For example...
var Mod = Backbone.Model.extend({});
var Col = Backbone.Collection.extend({ model: Mod });
var col = new Col([
{ name: "andy" },
{ name: "chad" },
{ name: "ashley" },
{ name: "louis" }
]);
col.comparator = function(p1, p2) {
return p1.get('name') < p2.get('name') ? -1
: (p1.get('name') > p2.get('name') ? 1 : 0 );
}
col.sort();
col.comparator = false;
// throws, was hoping this would return
// the collection to insertion order
col.sort();
Please disregard the fact that the comparator function I am setting here could be replaced with the sortBy implementation. This is just a contrived example. It is clear to me from the Backbone source that if you try and sort a collection with no comparator, it will throw:
// ... from BB source ...
sort: function(options) {
if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
// ...
Is there any way to return the order of the collection to insertion order?
model.set({originalOrder: num++})then sort later based on this... But yes, @Loamhoof is right if the collection is in an arbitrary order. - Paul Hoenecke