2016-01-22 83 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