过滤收藏集不起作用 [英] Filtering the collection doesn't work
问题描述
我有一个布尔值为interviewed
的模型集合,我想过滤它们,以便我的视图将显示此属性设置为true
的模型或该属性设置为false
的模型.在我的收藏夹中,我有以下方法:
I have a collection of models with boolean value interviewed
and I want to filter them so that my view would display either models with this property set to true
or models with this property set to false
. In my collection I have the following methods:
var ResumeCollection = Backbone.Collection.extend({
filterActive: function () {
var active = this.where({interviewed: false});
return active;
},
filterInterviewed: function () {
var interviewed = this.where({interviewed: true});
return interviewed;
}
});
在我看来,我有以下内容:
and in my view I have the following:
var ResumeList = Backbone.View.extend({
events: {
'click #active': 'showActive',
'click #interviewed': 'showInterviewed'
},
initialize: function () {
this.collection = new ResumeCollection();
},
render: function () {
var self = this;
this.$el.html( $('#filter') );
_.each(this.collection.toArray(), function (cv) {
self.$el.append((new ResumeView({model: cv})).render().$el);
});
},
showActive: function () {
this.collection.filterActive();
this.render();
},
showInterviewed: function () {
this.collection.filterInterviewed();
this.render();
}
});
但是,每当我单击#active
或#interviewed
按钮时,都不会发生任何事情,并且不会呈现具有所需属性的模型.我已经尝试使用reset
方法来管理该问题,或者使用所需的模型返回一个新的集合实例,但这不是解决方案,因为当我成功过滤初始集合时,它将返回一个包含所需模型的新集合(例如,其中interviewed: true
的模型),而我无法对其进行更多过滤-它仅返回一个空集合.
我只是无法了解如何以所需的方式过滤此一个集合.
But any time I click #active
or #interviewed
buttons, it happens nothing and the models with required properties aren't rendered. I've already tried to manage that with reset
method or returning a new collection instance with required models, but that's not a solution, because when I succesfully filter the initial collection, it returns me a new collection with the models I need (e.g. models where interviewed: true
), and I can't filter it more -- it returns just an empty collection.
I just can't get how can I filter this one collection in the way I need.
推荐答案
您将返回已成功过滤的集合,然后对它们不执行任何操作.
You're returning a successfully filtered collection, and then not doing anything with them.
showActive: function () {
this.collection.filterActive();//returns a value you're not using
this.render();
},
showInterviewed: function () {
this.collection.filterInterviewed();//returns a value you're not using
this.render();
}
我建议在您的render方法中添加一个可选参数,该参数代表过滤后的集合.如果定义了参数,请使用它.如果没有,请使用未过滤的集合.
I suggest adding an optional parameter to your render method that represents the filtered collection. If the parameter is defined, use it. If not, use the unfiltered collection.
借用@Simo的一些代码以返回新集合.
Borrowing some of @Simo's code to return a new collection.
filterActive: function () {
var active = this.where({interviewed: false});
return new ResumeCollection(active);
},
filterInterviewed: function () {
var interviewed = this.where({interviewed: true});
return new ResumeCollection(interviewed);
},
render: function (filtered) {
var self = this;
var data = filtered ? filtered.toArray() : this.collection.toArray();
this.$el.html( $('#filter') );
_.each(data , function (cv) {
self.$el.append((new ResumeView({model: cv})).render().$el);
});
},
showActive: function () {
var filtered = this.collection.filterActive();
this.render(filtered);
},
showInterviewed: function () {
var filtered = this.collection.filterInterviewed();
this.render(filtered);
}
这篇关于过滤收藏集不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!