主干收集排序依据

Backbone collection sortBy

本文关键字:排序      更新时间:2023-09-26

我制作了我的第一个主干应用程序,在集合排序方面遇到了一些问题。使用此后

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

console.log(SortedFriends)显示SortedFriend包含已排序的模型,但当我尝试使用诸如"SortedFridays.each"或"SortedFriends.at"之类的集合函数时,它会出错:

TypeError:SortedFriends.each不是函数。

代码:

  var Friend = Backbone.Model.extend({});          
      var Friends = Backbone.Collection.extend({
        model: Friend,            
      });  
      var MyFriends = new Friends();
      MyFriends.reset(<?=$friends?>);
      var FriendView = Backbone.View.extend({
          initialize: function(){
              model:Friend            
          },              
          tagName: "tr",
          template: _.template($('#item-template').html()),
          className: "document-row",          
          render: function() {                              
         this.$el.html(this.template(this.model.toJSON()));                            
              return this;             
          }          
      });     

    var SortedFriends = MyFriends.sortBy(function(friend) {
      return friend.get("uid");
    });          
    var addOne = function(element){           
        var view = new FriendView({model: element});
        $("#friends").append(view.render().el);
    }                              
    console.log(JSON.stringify(SortedFriends));
    SortedFriends.each(function(friend){
        var view = new FriendView({model: friend});
        $("#friends").append(view.render().el);            
    });

如果您使用主干集合,那么您可能最好使用comparator而不是收集方法

http://backbonejs.org/#Collection-比较器

当你准备好对你的收藏进行分类时:

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

或者,如果你想保持未排序集合的顺序,那么你需要首先克隆

http://backbonejs.org/#Collection-克隆

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

我不确定这是一个bug还是Backbone对sortBy的改编功能,但显然它返回了一个数组,而不是Undercore集合。

一种解决方法是将整个数组包装在_( ... )中,这告诉Underscore将数组包装回一个集合:

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

编辑

Backbone中的大多数Undercore方法似乎都是可链接的(例如,用reject替换sortBy,然后它就会运行)。从他们连接Undercore代理的主干源来看,sortBy似乎受到了不同的对待。我不明白他们为什么这样做。。。

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);
};