2016-01-22 5 views
-1

У меня есть вид Backbone, который выглядит так:Backbone 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 
    })); 
    } 
}); 

, когда я пытаюсь сделать это Backbone возвращает мне следующую ошибку:

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

Смежные вопросы