Created
March 21, 2015 23:27
-
-
Save alvinsng/c63043ee8393b510fee3 to your computer and use it in GitHub Desktop.
AltManagerContainer.js
This file contains hidden or 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 React = require('react'); | |
| module.exports = React.createClass({ | |
| displayName: 'AltManagerContainer', | |
| propTypes: { | |
| Action: React.PropTypes.function.isRequired, | |
| Store: React.PropTypes.function.isRequired, | |
| alt: React.PropTypes.object.isRequired, | |
| }, | |
| componentWillMount: function() { | |
| // we need to init the store and actions during creation | |
| this.initProps(this.props) | |
| }, | |
| componentWillReceiveProps: function(newProps) { | |
| // unlisten from old alt store if change of alt | |
| if (this.props.alt !== newProps.alt) { | |
| this.store.unlisten(this.listenState) | |
| // init the new props | |
| this.initProps(newProps) | |
| } | |
| }, | |
| listenState: function(data) { | |
| this.forceUpdate(); | |
| }, | |
| initProps: function(props) { | |
| var Store = this.props.Store | |
| var Action = this.props.Action | |
| if (!Store) { | |
| throw new ReferenceError('props.Store has not been defined') | |
| } | |
| if (!Action) { | |
| throw new ReferenceError('props.Action has not been is defined') | |
| } | |
| var actionName = Action.name | |
| if (!props.alt.getActions(actionName)) { | |
| props.alt.addActions(actionName, Action) | |
| props.alt.createStore(Store) | |
| } | |
| this.store.listen(this.listenState) | |
| this.forceUpdate(); | |
| }, | |
| componentWillUnmount: function() { | |
| this.store.unlisten(this.listenState) | |
| } | |
| render: function() { | |
| return this.props.children; | |
| }, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment