optimizely / nuclear-js

Reactive Flux built with ImmutableJS data structures. Framework agnostic.
https://optimizely.github.io/nuclear-js/
MIT License
2.23k stars 141 forks source link

[Question] How do dispatch one action within another action? #207

Closed mtermoul closed 8 years ago

mtermoul commented 8 years ago

I have the following senario:

onApplicationOpen ---> dispatch('ACTION1') ---> dispatch('ACTION2') ---> dispatch('ACTION3')

so ideally this should work, but somehow the state updates inside the ACTION3 does not update the status!

am I missing something here?

raulmatei commented 8 years ago

Have you tried to do a batched dispatch?

   reactor.batch(function () {
      reactor.dispatch('ACTION1');
      reactor.dispatch('ACTION2');
      reactor.dispatch('ACTION3');
   });

This will ensure that all listeners will be notified once, after the last dispatch has ended.

Raul.

mtermoul commented 8 years ago

Yes, I tired that. I was also to call one action from another using jquery promises. So as long as the dispatch is wrapped in a promise or setTimeout then you're cool.