骨幹

2016-01-22 107 views
0

在我的應用程序搜索收藏我有骨氣的收集,看起來像這樣:骨幹

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 

我做錯了嗎?

回答

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

我不建議這樣做。這是調用集合的構造函數,並使cpu爲編碼風格做了比必要工作更多的工作。 –

+0

嗯,那麼我怎樣才能以其他方式解決這個問題呢? –

+1

只需使方法接受可選參數(模型數組)。調用第一個方法,將結果傳遞給下一個方法。如果可選參數未傳遞,則讓方法過濾集合本身...您可以在生成的模型數組上使用'_.where' –

1

可能byReportOrganizationType失敗,因爲byReportType返回滿足條件(報告類型)的模型,但它不會返回Backbone.Collection,而是返回一組模型。這顯然陣沒有byReportOrganizationType功能定義

+0

因此,如何將在骨幹網解決這個問題的最好方法? –

+1

sry我錯過了你的問題,但顯然你設法做到了。 :) – djaszczurowski