Extend the listener mixin to provide "initial" data #44

Closed
spoike opened this Issue Aug 13, 2014 · 3 comments

Comments

Projects
None yet
2 participants
@spoike
Member

spoike commented Aug 13, 2014

Add the possibility to fetch initial data from a data store to a React component. This needs a change to the ListenerMixin's listenTo method and in the store implementation as well.

Motivating Example

var aStore = Reflux.createStore({
    init: function() {
        this.listenTo(anAction, function() {
            this.trigger('not initial data anymore');
        });
    }
    getInitialData: function() {
        return "the initial data";
         // is used by Listenermixin if a component wants initial data 
    }
});

var Component = React.createClass({
    mixins: [Reflux.ListenerMixin],
    getInitialState: function() {
        return {}; // <-- impossible to know the state if stores aren't initialized yet
    },
    componentDidMount: function() {
        this.listenTo(aStore, changeCallback, changeCallback);
            // will call changeCallback twice, during store change and 
            // when the store has initialized
            // third callback should be optional
    },
    changeCallback: function(data) {
        console.log(data);
        this.setState({
            data: data
        });
    }
});

In the example, before invoking the anAction the component should be able to set the state with "the initial data" fetched from the store's getInitialData method. When invoking the anAction the component should be able to set the state with "not initial data anymore" fetched from the change event.

Thoughts?

@spoike spoike added the enhancement label Aug 13, 2014

@KyleAMathews

This comment has been minimized.

Show comment
Hide comment
@KyleAMathews

KyleAMathews Aug 13, 2014

Contributor

Interesting. Could getInitialData return a promise? That might be useful for server fetching. Actions are "held" until the promise is returned.

Contributor

KyleAMathews commented Aug 13, 2014

Interesting. Could getInitialData return a promise? That might be useful for server fetching. Actions are "held" until the promise is returned.

@spoike

This comment has been minimized.

Show comment
Hide comment
@spoike

spoike Aug 15, 2014

Member

Probably should rename it to get "default" data. The point is more or less how to handle initialization of components, well before data comes in.

Member

spoike commented Aug 15, 2014

Probably should rename it to get "default" data. The point is more or less how to handle initialization of components, well before data comes in.

@undoZen undoZen referenced this issue Aug 18, 2014

Merged

Get default data #49

@spoike spoike added this to the 0.1.7 milestone Aug 20, 2014

@spoike

This comment has been minimized.

Show comment
Hide comment
@spoike

spoike Aug 20, 2014

Member

Closing issue as it is merged in #46 and #49

Member

spoike commented Aug 20, 2014

Closing issue as it is merged in #46 and #49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment