在我的應用程序搜索收藏我有骨氣的收集,看起來像這樣:骨幹
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'))
骨幹返回我以下錯誤:
TypeError: this.collection.byReportType(...).byReportOrganizationType is not a function
我做錯了嗎?
我不建議這樣做。這是調用集合的構造函數,並使cpu爲編碼風格做了比必要工作更多的工作。 –
嗯,那麼我怎樣才能以其他方式解決這個問題呢? –
只需使方法接受可選參數(模型數組)。調用第一個方法,將結果傳遞給下一個方法。如果可選參數未傳遞,則讓方法過濾集合本身...您可以在生成的模型數組上使用'_.where' –