Skip to content

Instantly share code, notes, and snippets.

@andreisebastianc
Created June 30, 2014 04:04
Show Gist options
  • Save andreisebastianc/1c80258b81b520b231cb to your computer and use it in GitHub Desktop.
Save andreisebastianc/1c80258b81b520b231cb to your computer and use it in GitHub Desktop.
define([], function () {
return {
bindTo: function(model, key){
return {
value: model.get(key),
requestChange: function(value){
model.set(key, value);
}.bind(this)
}
}
}
});
define([], function () {
return {
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) this.__syncedModels = [];
if(!~this.__syncedModels.indexOf(model)){
var updater = this.forceUpdate.bind(this, null);
model.__updater = updater;
model.on('add change remove', updater, this);
this.__syncedModels.push(model);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment