Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React dispatcher WAITFOR

Tags:

reactjs-flux

I'm trying to use the waitFor function of react.js but it seems I'm doing something wrong.

What I want to do i basic, wait for a store to be filled before calling it from another store. 1.Register token in the first store

RipplelinesStore.dispatcherIndex= Dispatcher.register(function(payload) {
    var action = payload.action;
    var result;

    switch(action.actionType) {

         case Constants.ActionTypes.ASK_RIPPLELINES:    
            registerAccount(action.result); 
            RipplelinesStore.emitChange(action.result);         
            break;
    }

});

2.Write the wait for in the other store

Dispatcher.register(function(payload) {
    var action = payload.action;
    var result;

    switch(action.actionType) {
        case Constants.ActionTypes.ASK_RIPPLEACCOUNTOVERVIEW:
            console.log("overviewstore",payload);
            Dispatcher.waitFor([
                RipplelinesStore.dispatcherIndex,
            ]);

            RippleaccountoverviewsStore.test= RipplelinesStore.getAll();
            console.log(RippleaccountoverviewsStore.test);

            break;
    }

    return true;
});

Unfortunately my getall() method return an empty object (getAll() is well written). So it seems that the waitFor dispatcher function is not working.

Basically I know that's because the first store is still receiving the answer from the server but I thought that waitFor would waitfor it to be fetched I don't get it.

Any clue ? Thanks!

Edit: I fire the first store fetch like tha. What I don't understand is that I'm dispatching the load once my backbone collection has fetched (I dispatch on succeed with a promise...)

ripplelinescollection.createLinesList(toresolve.toJSON()).then(function() { 
            Dispatcher.handleViewAction({
                actionType: Constants.ActionTypes.ASK_RIPPLELINES,
                result: ripplelinescollection
            });
        }); 

I also tried to bind the waitfor to an action which is never called but the other store is still not waiting ! WEIRD !

like image 218
François Richard Avatar asked Jan 26 '15 09:01

François Richard


1 Answers

seems like the problem is the async fetch from the server. waitFor isn't supposed to work this way. You will have to introduce another action that is triggered as soon as the data has been received from the server.

Have a look at this answer: https://stackoverflow.com/a/27797444/1717588

like image 186
FranBran Avatar answered Sep 17 '22 14:09

FranBran