Last active
August 29, 2015 14:03
-
-
Save bimbiero/36e1de9062f4b7cc7554 to your computer and use it in GitHub Desktop.
BackboneMixin for updating react on model events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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