在我的应用程序中,我有Backbone集合,如下所示:
App.Collections.SurveyReportSets = Backbone.Collection.extend({
url: '/survey_report_sets',
model: App.Models.SurveyReportSet,
byReportType: function(report_type) {
return this.where({report_type: report_type});
},
byReportOrganizationType: function(report_organization_type) {
return this.where({report_organization_type: report_organization_type});
}
});
当我只使用其中一个时,此搜索工作正常。但是,当我尝试使用它们时,它们不起作用。以下是我使用它的方式:
var my_collection = this.collection.byReportType(this.model.get('report_type')).byReportOrganizationType(this.model.get('report_organization_type'))
Backbone返回以下错误:
TypeError: this.collection.byReportType(...).byReportOrganizationType is not a function
我做错了什么?
答案 0 :(得分:1)
可能byReportOrganizationType
失败,因为byReportType
返回满足条件(报告类型)的模型,但它不会返回Backbone.Collection而是一组模型。该数组显然没有定义byReportOrganizationType
函数
答案 1 :(得分:0)
我已经更新了返回集合的方法并且它正常工作:
App.Collections.SurveyReportSets = Backbone.Collection.extend({
url: '/survey_report_sets',
model: App.Models.SurveyReportSet,
byReportType: function(report_type) {
return new App.Collections.SurveyReportSets(this.where({report_type: report_type}));
},
byReportOrganizationType: function(report_organization_type) {
return new App.Collections.SurveyReportSets(this.where({report_organization_type: report_organization_type}));
}
});