Skip to content

Instantly share code, notes, and snippets.

@designeng
Last active December 22, 2015 12:06
Show Gist options
  • Save designeng/4bf9ed3e56b4d7127406 to your computer and use it in GitHub Desktop.
Save designeng/4bf9ed3e56b4d7127406 to your computer and use it in GitHub Desktop.
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" />;
}
};
};
}
import React, { PropTypes } from 'react';
import connectModel from './../source/connectModel';
@connectModel({
sourcePath : '/navigation/model.json',
getValue : 'items'
})
export default class Navigation extends React.Component {
constructor() {
super();
this.state = {items: []};
}
render() {
let items = this.state.items
items = Object.keys(items).map(idx => {
return <li key={ idx }>
<a href={ "#" + items[idx].href } >
{ items[idx].name }
</a></li>;
});
return <ul>{ items }</ul>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment