React dispatcher WAITFOR
Asked Answered
B

1

10

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 !

Budde answered 26/1, 2015 at 9:31 Comment(4)
How exactly are you fetching it form the server? Stores are synchronous, if you fire a request inside the store's callback, other stores don't wait for it.Leucocratic
I fetch it from my actions controller, i edited my postMosemoseley
It looks like your code is testing different action constants in both controllers. Constants.ActionTypes.ASK_RIPPLELINES, Constants.ActionTypes.ASK_RIPPLEACCOUNTOVERVIEW.Leucocratic
possible duplicate of Flux waitFor() and async operation, how to model.Disgraceful
C
9

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://mcmap.net/q/1166341/-flux-waitfor-and-async-operation-how-to-model

Chomp answered 23/2, 2015 at 16:57 Comment(2)
Indeed the "waitfor" is confusing. It should be called "CheckThisStore", or "GetLastStoreResult" because in fact it's not "waiting" for anythingMosemoseley
Jesus waterwalking Christ what a brilliant idea to name an API in a way that suggests it does something entirely different than what it actually does...Rodin

© 2022 - 2024 — McMap. All rights reserved.