In a Flux app, what's the right way handle API requests other than the initial data load?

186 views Asked by At

When using Flux, what's the right way handle AJAX requests other than the initial data load?

I'm building basic twitter-clone; I have an API utility that loads the initial data and then calls an action (tweetActions.loadTweets(data)), which the TweetStore is listening for.

But what's the pattern for, say, when a user creates a new tweet? Right now, I have the React view class calling an action (tweetActions.tweet(data)), and the API utility listening for that action. The utility then makes the API request, and when the tweet is successfully saved to the server, it calls another action (tweetActions.savedTweet(data)). The TweetStore listens for that, and then adds it to the store, emits a change event, and the UI is updated.

Is there a better what to do this? I haven't seen any examples where an API utility registers a function with the dispatcher, but I'm not sure how else to do this.

1

There are 1 answers

1
rooftop On BEST ANSWER

What do you mean by this statement?

and the API utility listening for that action

Get the flux diagram and just follow the arrows. The web api utils should NOT be talking to the dispatcher they should be invoked from actions. When requests are finished, they need to also invoke an action that let's the rest of the system know the request was successful or failed.

http://i.imgur.com/vrkMrGQ.png