简体   繁体   中英

React + Flux: Action triggers synchronous ajax requests

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM