Skip to content

Instantly share code, notes, and snippets.

@aaronj1335
Created October 17, 2014 13:46
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 aaronj1335/f9d80b6d89712d32733d to your computer and use it in GitHub Desktop.
Save aaronj1335/f9d80b6d89712d32733d to your computer and use it in GitHub Desktop.
module.exports = {
listenTo: function(other, event, callback) {
other.on(event, callback, this);
this._eventEmitters = this._eventEmitters || [];
this._eventEmitters.push([other, event]);
},
componentWillUnmount: function() {
(this._eventEmitters || []).forEach(function([other, event]) {
other.off(event, null, this);
});
}
};
var shortcodes = require('./../../data/shortcodes');
var ListenTo = require('./../../util/listen-to');
module.exports = createClass({
displayName: 'MyComponent',
mixins: [ListenTo],
getInitialState: function() {
return {
models: shortcodes().toJSON()
};
},
componentDidMount: function() {
this.listenTo(shortcodes(), 'all', this.onEvent);
shortcodes().initialFetch();
},
onEvent: function() {
this.setState({models: shortcodes().toJSON()});
},
render: function() {
return /* ... */;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment