Skip to content

Instantly share code, notes, and snippets.

@bimbiero
Last active August 29, 2015 14:03
Show Gist options
  • Save bimbiero/36e1de9062f4b7cc7554 to your computer and use it in GitHub Desktop.
Save bimbiero/36e1de9062f4b7cc7554 to your computer and use it in GitHub Desktop.
BackboneMixin for updating react on model events
define(function() {
return {
componentDidMount: function () {
// Whenever there may be a change in the Backbone data, trigger a
// reconcile.
this.getBackboneModels().forEach(function (model) {
// explicitly bind `null` to `forceUpdate`, as it demands a callback and
// React validates that it's a function. `model` events passes
// additional arguments that are not functions
model.on('sync change destroy', this.forceUpdate.bind(this, null), this);
}, this);
},
componentWillUnmount: function () {
// Ensure that we clean up any dangling references when the component is
// destroyed.
this.getBackboneCollections().forEach(function (model) {
model.off(null, null, this);
}, this);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment