Search code examples
javascriptbackbone.jsbackbone.js-collections

Searching collections in backbone


In my app I have Backbone collection that looks like this:

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

This searches works perfectly when I use only one of them. But when I try to use both they not works. Here is how I'm using it:

var my_collection = this.collection.byReportType(this.model.get('report_type')).byReportOrganizationType(this.model.get('report_organization_type'))

Backbone returns me following error:

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

What I'm doing wrong?


Solution

  • I've updated method to returns collection and it works properly:

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