在骨干中搜索集合

时间:2016-01-22 09:07:48

标签: javascript backbone.js backbone-collections

在我的应用程序中,我有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

我做错了什么?

2 个答案:

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