Skip to content

Instantly share code, notes, and snippets.

@mrjoes
Created December 2, 2013 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjoes/7751231 to your computer and use it in GitHub Desktop.
Save mrjoes/7751231 to your computer and use it in GitHub Desktop.
var BackboneMixin;
BackboneMixin = {
componentDidMount: function(node) {
if (this.props.model) {
return this.watchBackbone(this.props.model);
}
},
componentWillUnmount: function() {
return this.unwatchBackboneAll();
},
watchBackbone: function(model, events, handler) {
var changeOptions, fn, update;
if (events == null) {
events = ' sync remove reset';
}
if (!model) {
throw "Specify model to watch, received '" + model + "'";
}
this._watching || (this._watching = []);
if (model instanceof Collection) {
update = handler || (function() {
return this.forceUpdate();
});
model.on(events, update, this);
} else if (model) {
changeOptions = this.changeOptions || 'change';
fn = this.onModelChange || this.forceUpdate;
model.on(changeOptions, (function() {
return fn.call(this);
}), this);
}
return this._watching.push(model);
},
unwatchBackboneAll: function() {
var m, _i, _len, _ref;
if (!this._watching) {
return;
}
_ref = this._watching;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
m = _ref[_i];
m.off(null, null, this);
}
return delete this._watching;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment