0

I am building a survey style app using React + Flux. When a user submits an answer I fire off an action that triggers an ajax request to save the answer in the database (ajax request #1). I then need to do an ajax request to get the next question (ajax request #2).

Request #2 depends on the data that #1 writes to the database. I'd like to avoid returning the next question when creating the answer. Any suggestions on where/how best to trigger #2 so that we can be assured #1 has finished?

Thanks

Nathan
  • 869
  • 2
  • 8
  • 7
  • Is there any reason you want to avoid returning the next question when creating the answer? – eddyjs Aug 04 '15 at 02:01
  • Mainly just to make each API endpoint be a single concern. This is probably the route I will end up taking though unless anything better comes along. – Nathan Aug 04 '15 at 16:10

1 Answers1

0

Within your Ajax success function, you can let the AppDispatcher announce something like QUESTION_ANSWERED, passing the question number.

A store can now listen for that event, set the just-answered question, and emit its CHANGE event.

Your survey component can then listen for this event and, when it receives a notification from the store, create a new action to get the next question.

That gets you separate endpoints (which I agree with you is preferable).

HTH

Hal Helms
  • 684
  • 3
  • 8