Created
January 27, 2014 17:17
-
-
Save erikakers/8653019 to your computer and use it in GitHub Desktop.
React.js Backbone Mixins
This file contains hidden or 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
| var ModelMixin = { | |
| __syncedModels: [], | |
| componentDidMount: function() { | |
| // Whenever there may be a change in the Backbone data, trigger a reconcile. | |
| this.getBackboneModels().forEach(this.injectModel, this); | |
| }, | |
| componentWillUnmount: function() { | |
| // Ensure that we clean up any dangling references when the component is | |
| // destroyed. | |
| this.__syncedModels.forEach(function(model) { | |
| model.off(null, model.__updater, this); | |
| }, this); | |
| }, | |
| injectModel: function(model){ | |
| if(!~this.__syncedModels.indexOf(model)){ | |
| var updater = this.forceUpdate.bind(this, null); | |
| model.__updater = updater; | |
| model.on('add change remove', updater, this); | |
| } | |
| }, | |
| bindTo: function(model, key){ | |
| return { | |
| value: model.get(key), | |
| requestChange: function(value){ | |
| model.set(key, value); | |
| }.bind(this) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment