2016-01-22 84 views
-1

我有一個看起來像這樣的骨幹觀點:骨幹的ReferenceError

App.Views.HistoricalDataSelector = Backbone.View.extend({ 
    initialize: function (options) { 
    this.template = JST['backbone/templates/historical_data_selector']; 
    this.collection = options.collection; 
    this.model = options.model; 
    this.render(); 
    }, 

    myCollection: function() { 
    return this.collection.byReportType(this.model.get('report_type')).byReportOrganizationType(this.model.get('report_organization_type')) 
    }, 

    render: function() { 
    this.$el.html(this.template({ 
     collection: myCollection, 
     model: this.model 
    })); 
    } 
}); 

當我試圖使其骨幹返回我以下錯誤:

ReferenceError: myCollection is not defined 

但是當我改變渲染方法如下:

render: function() { 
    this.$el.html(this.template({ 
     collection: this.collection.byReportType(this.model.get('report_type')).byReportOrganizationType(this.model.get('report_organization_type')), 
     model: this.model 
    })); 
    } 

他爲什麼找不到這個myCollection方法?

回答

6

你忘了this關鍵字,大概也調用方法:

render: function() { 
this.$el.html(this.template({ 
    collection: this.myCollection(), 
    //-----------^ view reference ^---- function invocation ------ 
    model: this.model 
})); 
} 
+0

這將指向當前視圖對象。 – Prateek