Last active
December 22, 2015 12:06
-
-
Save designeng/4bf9ed3e56b4d7127406 to your computer and use it in GitHub Desktop.
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
| import Falcor from "falcor"; | |
| import FalcorDataSource from 'falcor-http-datasource'; | |
| import React, { PropTypes } from "react"; | |
| export default function(config) { | |
| if (!config.sourcePath) { | |
| throw new Error('Falcor model sourcePath should be provided!'); | |
| } | |
| const model = new Falcor.Model({ | |
| source: new FalcorDataSource(config.sourcePath) | |
| }); | |
| return function(component) { | |
| const Component = component; | |
| Component.contextTypes = { | |
| model: PropTypes.object.isRequired | |
| }; | |
| return class ConnectModelComponent extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { model: model }; | |
| } | |
| static childContextTypes = { | |
| model: React.PropTypes.object.isRequired | |
| }; | |
| getChildContext() { | |
| return { model: this.state.model }; | |
| } | |
| componentWillMount() { | |
| if (config.getValue) { | |
| model.getValue([config.getValue]) | |
| .then(response => { | |
| this.refs.childComponent.setState({ [config.getValue]: response }); | |
| }) | |
| } | |
| } | |
| render() { | |
| return <Component {...this.props} ref="childComponent" />; | |
| } | |
| }; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment