React.js,如何在组件外部绑定 `this`

React.js, how to bind `this` outside of component

我有 backbone 的混合反应。我试图在 changeSeasons 方法中使用 this(或者那个,自我,等等)来访问 backbone 视图方法(在 HomeView 中)。但是因为 changeSeasons 是在 HomeMainComp 组件中调用的,所以 this 被绑定到 react 组件。我怎样才能正确绑定 this 以便我可以在我的 changeSeasons 方法中访问 Backbone 视图的方法?

HomeView = Backbone.View.extend({

  initialize: function(){
      // init stuff
      this.fetchData();
  },

  fetchData: function(){
      // fetch stuff then runs renderReact...
  },

  renderReact: function(){
    React.render(
      <HomeMainComp
        changeSeasons={this.changeSeasons}
        probablePitchers={this.probablePitchers.toJSON()} />,
        document.getElementById('app')
      );
  },

  changeSeasons: function(seasons){
      console.log(this); // shows the HomeMainComp...,
      this.pitcherStats.setSeasons(seasons); // so this don't work
      this.fetchData(); // this don't work either
  },

    ...

})

编辑:根据下面的一些建议,我可以通过将 (null, this) 绑定到 changeSeasons 来获得 HomeView 作为我的 this,但是我需要传入 this我的 changeSeasons 方法与另一个绑定?我有点搞不懂这是怎么回事,在这种情况下,我无法再访问传入变量 seasons.

  renderReact: function(){

    React.render(
      <HomeMainComp
        changeSeasons={this.changeSeasons.bind(null, this)}
        probablePitchers={this.probablePitchers.toJSON()} />,
        document.getElementById('app')
            );
     },

  changeSeasons: function(_this){
    console.log('this: ', _this) ## this gives me the HomeView object
    console.log('Season change: ', seasons); ## but now I'm having trouble accessing my incoming seasons variable, which is empty because _this is taking the space.
      _this.pitcherStats.setSeasons(seasons);
      _this.fetchData();
  }.bind(this),

您可以在呈现组件时绑定 changeSeasons

  renderReact: function(){
    React.render(
      <HomeMainComp
        changeSeasons={this.changeSeasons.bind(this)}
        probablePitchers={this.probablePitchers.toJSON()} />,
        document.getElementById('app')
      );
  },

这会在每次调用 renderReact 时创建一个新函数。虽然可能没什么大不了的,但如果你想最小化函数 creation/GC,你可以更早地绑定它:

  initialize: function(){
      // init stuff
      this.changeSeasons = this.changeSeasons.bind(this);
      this.fetchData();
  },

  // ...

  renderReact: function(){
    React.render(
      <HomeMainComp
        changeSeasons={this.changeSeasons}
        probablePitchers={this.probablePitchers.toJSON()} />,
        document.getElementById('app')
      );
  },

由于提到的 mu 太短,Underscore 提供了一个方便的函数来将一个或多个方法绑定到一个对象:

  initialize: function(){
      // init stuff
      _.bindAll(this, "changeSeasons");
      this.fetchData();
  },