使用 id 数组过滤和排序主干集合 [英] Filter and sort backbone collection with array of ids
问题描述
我对 Backbone 很陌生,所以我遇到了一些我无法弄清楚的问题.
I'm quite new to Backbone so I am getting into some problems I can't quite figure out.
我有一个 Backbone 系列,里面有 100 多个项目.我想用一组 id 过滤这些,这工作正常,但我希望项目的顺序也基于这个数组的项目顺序.那是行不通的.其他排序方法似乎是基于 asciibetical,这也不是我需要的.是否可以使用此过滤器获取项目,然后按照我定义的顺序将它们放入集合中?
I have a Backbone collection with a bit over 100 items. I want to filter these with an array of ids, that is working fine, but I want the order of the items also based on this array's order of items. That is not working. The other sorting methods seems to be asciibetical based, that's not what I need either. Is it possible to get items using this filter, and then also put them into the collection in the order I've defined?
我有一个用来过滤的 id 数组,这个数组看起来像这样:
I have an array of id's that I filter with, this array looks like this:
var dDefaultItems = ['1','2','146','3','4','9','26','8','96','10','11','54','145','273','38'];
收集和过滤的代码如下所示:
The code for the collection and filtering looks like this:
var ChannelCollection = Backbone.Collection.extend({
fetch : function() {
var params = _.extend({}, arguments, {
data : {
"groupnumber" : "1000"
}
});
this.constructor.__super__.fetch.apply(this, [params]);
},
model : Channel,
url : function () {
return utility.apiUrl('/myurl/tothething');
},
filterData: function(params) {
this.originalModels = this.models.slice();
_.each(params, function(val, key){
if (typeof val !== 'object') val = [ val ];
this.models = _.filter(this.models, function(model){
return _.indexOf(val, model.get(key)) !== -1;
}, this);
}, this);
return this.reset(this.models).toJSON();
},
parse : function(json) {
return json.channelInfoList;
}
});
然后我使用此代码在视图中呈现它(还有其他一些代码用于定义我认为不相关的模型和其他属性,我可能错了,但我想有人会知道我需要什么从看这个.)
Then I render this in a view with this code (there's other bits of code for defining model and other attributes that I don't think is relevant, I may be wrong, but I'm thinking someone will know what I need from looking at this.)
var ChannelListView = Backbone.View.extend({
initialize: function() {
var _this = this;
currentChannelList = new ChannelCollection();
currentChannelList.once("sync", function() {
_this.render();
});
currentChannelList.fetch();
},
render : function() {
var _this = this;
$(_this.el).empty();
dust.render("list-channels", { channelList : currentChannelList.filterData({id: dDefaultItems})} , function(err, html) {
var $el = $(html).appendTo($(_this.el));
});
}
});
推荐答案
Backbone 集合会自动按插入顺序排序,除非您实现了 Collection#comparator
.问题是你的过滤算法没有产生有序的输出.
Backbone collections are automatically sorted by the order of insertion, unless you implement Collection#comparator
. The problem is that your filtering algorithm is not producing an ordered output.
如果你只需要在按 id 过滤时维护一个有序集合,我建议实现一个单独的方法,因为与按任意属性搜索相比,按 id 搜索要快得多:
If you need to maintain an ordered collection only when filtering by id, I would suggest implementing a separate method, because seach by id is far faster compared to search by arbitrary attributes:
filterById: function(idArray) {
return this.reset(_.map(idArray, function(id) { return this.get(id); }, this));
}
用法:
collection.filterById(['1', '2', '146', '3', '4', '9', '26', '8', '96', '10', '11', '54',' 145', '273', '38']);
这篇关于使用 id 数组过滤和排序主干集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!