2013-08-02 65 views
4

我做了我的第一个骨干应用程序,并收集排序问题。 使用该骨干收集sortBy

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

的console.log后(SortedFriends)显示,SortedFriends包含排序的模型,但是当我尝试使用收藏功能,如“SortedFriends.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);    
    }); 

回答

2

我不知道这是否是一个错误或sortBy骨干适应的功能,但显然它返回一个数组,而不是下划线集合。

一个解决办法是包装在_(...)整个事情,它告诉下划线到阵列回卷到一个集合:

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

编辑

大多数的骨干下划线方法似乎是可链接的(例如,将sortBy替换为reject,然后运行)。看他们连接下划线代理的主干源,似乎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); 
}; 
+1

此帮助 VAR SortedFriends = _(MyFriends.sortBy(函数(朋友){ 回报friend.get( “UID”); })); 谢谢! – fyodor

3

如果你使用的那么你是可能使用comparator更好,而不是收集方法收集骨干

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

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

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

或者如果您想保留未分类集合的顺序,那么您将需要克隆它首先

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

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