主干收集排序依据
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);
};
相关文章:
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 使用promise和mongoose对文档进行排序
- Selectize.js:如何对整数值的选项进行排序
- Javascript排序的图像弹出窗口..可以't单独弹出
- 如何通过引用var Using DataTables来进行分页或排序
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- AngularJs对所有页面中的所有记录进行排序
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- 按从高到低对多个int变量进行排序
- jQuery UI可排序-多连接列表拖动
- Javascript排序字符串或数字
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- sort而不是排序javascript
- 通过从节点父级获取所有子级对节点进行排序(获取子级数组)
- 如何合并不同集合的游标并按日期排序
- 为什么我的JavaScript堆栈排序函数不起作用
- ui网格:在自定义表头模板中触发排序
- jquery Onclick函数带有导致双击的回调排序函数
- Rubaxa可与聚合物进行排序/拖放不起作用,具体取决于显示器:
- 对角度数据表中括号内的数字进行排序