Skip to content

Instantly share code, notes, and snippets.

@erikakers
Created January 27, 2014 17:17
Show Gist options
  • Select an option

  • Save erikakers/8653019 to your computer and use it in GitHub Desktop.

Select an option

Save erikakers/8653019 to your computer and use it in GitHub Desktop.
React.js Backbone Mixins
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