Backbone ReferenceError

Mat*_*ski -1 backbone.js

我有Backbone视图,如下所示:

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
    }));
  }
});
Run Code Online (Sandbox Code Playgroud)

当我尝试渲染它Backbone返回以下错误:

ReferenceError: myCollection is not defined
Run Code Online (Sandbox Code Playgroud)

但是当我将渲染方法更改为:

  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
    }));
  }
Run Code Online (Sandbox Code Playgroud)

为什么他找不到这个myCollection方法?

T J*_*T J 6

您忘记了this关键字,也可能还要调用该方法:

render: function () {
 this.$el.html(this.template({
  collection: this.myCollection(),
  //-----------^ view reference ^---- function invocation ------
  model: this.model
 }));
}
Run Code Online (Sandbox Code Playgroud)