flux icon indicating copy to clipboard operation
flux copied to clipboard

action creator method must be called asynchronously

Open hon opened this issue 11 years ago • 4 comments

I've defined an action creator, and call some method direct in the view page (or in the constructor):

    //action creator
    MaintenanceActions.initCarInfo({
      data: [{test:'test'}],
      source: settings.fluxSource.cache
    });

and listen to the event:

    componentDidMount(){
      let initCallback = function(){
        console.log('init callback..');
      }
      //init
      MaintenanceStore.addInitListener(initCallback);
    }

the 'initCallback' function is not called. It seem that

  this.on('init_event', callback)

doesn't handle.

But, if I call 'MaintenanceActions.initCarInfo()' in an event handler, like click event, or a ajax callback, it works.

Must I call the action creator method asynchronously?

hon avatar Jul 27 '15 09:07 hon

I can't see any problem above your code. initCallback will call when you create a action

changzhiwin avatar Jul 27 '15 15:07 changzhiwin

You shouldn't need to call an action creator asynchronously. You will want to ensure that your store has registered itself with the dispatcher before calling your action creators synchronously though.

kyldvs avatar Jul 27 '15 18:07 kyldvs

Thanks, I should listen to the event, before the event is triggered:

  componentDidMount(){
    let initCallBack = function(){
      console.log('init callback..');
    }
    // listen here, before event is triggered.
    MaintenanceStore.addInitListener(initCallBack);

    //触发action
    MaintenanceActions.initCarInfo({
      data: [{test:'test'}],
      source: settings.fluxSource.cache
    });
  }

It works as expected.

hon avatar Jul 28 '15 04:07 hon

Awesome, glad you figured it out!

kyldvs avatar Jul 28 '15 06:07 kyldvs