handling backbone model/collection changes in react.js

后端 未结 5 1923
旧巷少年郎
旧巷少年郎 2020-12-23 17:00

I\'ve been working with facebooks framework React.js together with Backbone for the last couple of weeks and I\'m still not entirely sure what is the most appropriate way to

5条回答
  •  死守一世寂寞
    2020-12-23 17:30

    I had been playing around with the BackboneMixin mentioned here and a couple other react resources (of the limited info currently out there). I found that when I was listening to a collection that was being updated from the server, just as many n 'add' events are going to be triggered on the collection and listened to by the BackboneMixin, thus calling force update n number of times, which calls render and whatever is called from render n number of times.

    Instead, I used underscore/lo-dash's throttle method to limit the number of times forceUpdate would be called. At the very least this has limited the render method from being called so much. I know react isn't actually doing any DOM manipulation there, and its just a virtual DOM, but still there is no reason it should be called 100 times for 100 immediate additions to a Collection.

    So my solution looks like https://gist.github.com/ssorallen/7883081 but with the componentDidMount method like this instead:

    componentDidMount: function() {
      //forceUpdate will be called at most once every second
      this._boundForceUpdate = _.throttle(this.forceUpdate.bind(this, null), 1000);
      this.getBackboneObject().on("all", this._boundForceUpdate, this);
    }
    

提交回复
热议问题