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 !

François Richard
  • 6,817
  • 10
  • 43
  • 78
  • 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. – Mark Meeus Jan 26 '15 at 15:06
  • I fetch it from my actions controller, i edited my post – François Richard Jan 26 '15 at 16:40
  • It looks like your code is testing different action constants in both controllers. Constants.ActionTypes.ASK_RIPPLELINES, Constants.ActionTypes.ASK_RIPPLEACCOUNTOVERVIEW. – Mark Meeus Jan 27 '15 at 13:10
  • 1
    possible duplicate of [Flux waitFor() and async operation, how to model.](http://stackoverflow.com/questions/27785988/flux-waitfor-and-async-operation-how-to-model) – Gajus Jul 21 '15 at 13:32

1 Answers1

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://stackoverflow.com/a/27797444/1717588

Community
  • 1
  • 1
FranBran
  • 1,017
  • 2
  • 11
  • 32
  • 5
    Indeed the "waitfor" is confusing. It should be called "CheckThisStore", or "GetLastStoreResult" because in fact it's not "waiting" for anything – François Richard Jun 14 '15 at 00:27
  • 2
    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... – Szczepan Hołyszewski Jun 18 '16 at 14:08