Skip to content Skip to sidebar Skip to footer

Backbone Collection Sortby

I make my first backbone app and get some problems with collection sorting. After using this var SortedFriends = MyFriends.sortBy(function(friend) { return friend.get('u

Solution 1:

If youre using backbone collections then youre probably better off using the comparator rather than collection methods

http://backbonejs.org/#Collection-comparator

When youre ready to sort your collection:

MyFriends.comparator = function(friend){
   return friend.get("uid");
});
MyFriends.sort();

OR if you want to keep the order of the unsorted collection then you will need to clone it first

http://backbonejs.org/#Collection-clone

var SortedFriends = MyFriends.clone();
SortedFriends.comparator = function(friend){
   return friend.get("uid");
});
SortedFriends.sort();

Solution 2:

I'm not sure if it's a bug or a feature of Backbone's adaptation of sortBy, but apparently it returns an array, not an Underscore collection.

One workaround is to wrap the whole thing in _( ... ), which tells Underscore to wrap the array back into a collection:

varSortedFriends = _(MyFriends.sortBy(function(friend) {
  return friend.get("uid");
}));

Edit

Most of the Underscore methods in Backbone seem to be chainable (replace sortBy with reject, for example, and it runs). Looking at the Backbone source where they wire up the Underscore proxies, it seems that sortBy is treated differently. I can't understand why they do it this way ...

var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
    'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
    'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
    'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
    'tail', 'drop', 'last', 'without', 'indexOf', 'shuffle', 'lastIndexOf',
    'isEmpty', 'chain'];

_.each(methods, function(method) {
    Collection.prototype[method] = function() {
      var args = slice.call(arguments);
      args.unshift(this.models);
      return _[method].apply(_, args);
    };
});

var attributeMethods = ['groupBy', 'countBy', 'sortBy'];

_.each(attributeMethods, function(method) {
    Collection.prototype[method] = function(value, context) {
      var iterator = _.isFunction(value) ? value : function(model) {
        return model.get(value);
      };
      return _[method](this.models, iterator, context);
};

Post a Comment for "Backbone Collection Sortby"