Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getInititalData() to Store #46

Merged
merged 2 commits into from
Aug 20, 2014
Merged

Add getInititalData() to Store #46

merged 2 commits into from
Aug 20, 2014

Conversation

SimonDegraeve
Copy link
Contributor

Add the getInitialData() method to Store as mentioned in #44.
This method is called when an optional third argument is provided to listenTo in a React Component using the ListenerMixin.

Example:

var aStore = Reflux.createStore({
    init: function() {
        this.listenTo(anAction, function() {
            this.trigger('not initial data anymore');
        });
    }
    getInitialData: function() {
        return 'the initial data'; // Can also return a Promise
    }
});

var Component = React.createClass({
    mixins: [Reflux.ListenerMixin],
    getInitialState: function() {
        return {};
    },
    componentDidMount: function() {
        this.listenTo(
            aStore, 
            changeCallback, 
            changeCallback // This callback is optional, execute getInitialData from aStore
        );
    },
    changeCallback: function(data) {
        console.log(data);
        this.setState({
            data: data
        });
    }
});

TODO:

  • write the tests

initialCallback(data);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this part of the listenTo method (L24-34) is exactly the same in both createStore and ListenerMixin it'd be nice to have this extracted to a function in src/utils.js (in order to avoid double maintenance).

Something like _.handleInitialData(listenable, initialCallback).

@SimonDegraeve
Copy link
Contributor Author

Indeed. I will update it when I go home. Any other suggestions?

@spoike
Copy link
Member

spoike commented Aug 17, 2014

I'll accept this PR once there are tests for it. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants