在 backbone 中搜索合集

Searching collections in backbone

在我的应用程序中,我有 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 returns 我以下错误:

TypeError: this.collection.byReportType(...).byReportOrganizationType is not a function

我做错了什么?

可能 byReportOrganizationType 失败,因为 byReportType returns 满足条件(报告类型)的模型,但它不满足 return Backbone.Collection 但数组模型。这个数组显然没有定义byReportOrganizationType函数

我已将方法更新为 returns 集合并且它工作正常:

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}));
  }
});