Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //USAGE
- React.createClass({
- mixins: [
- ConnectModel(TaskStore, 'task', (t) => t.name == 'Ship Your Pets'), // <--pass finder function
- ConnectModel(TaskStore, 'task', 23) // <--OR pass id to use
- ]
- });
- // MIXIN DEFINITION
- export default function ConnectModel(store, key, finderFunc) {
- var fn = finderFunc;
- if(!_.isFunction(fn)) fn = (model) => model.id == finderFunc;
- var unsubscribe;
- return {
- getInitialState: function() {
- var model = _.find(store.models, fn);
- var state = {};
- state[key] = model;
- return state;
- },
- componentDidMount: function() {
- unsubscribe = store.listen(() => {
- var model = _.find(store.models, fn);
- if (!(model && model.isEqual(this.state[key]))) {
- var state = {};
- state[key] = model;
- this.setState(state);
- }
- });
- },
- componentWillUnmount: function() {
- unsubscribe();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement