Created
December 2, 2013 15:34
-
-
Save mrjoes/7751231 to your computer and use it in GitHub Desktop.
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
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