Flux + data lifecycle

In the FLUX application, a given dataset is populated in stores through an action such as initialization. What to do, if:

  • The store must be incrementally initialized. (Add users one by one).
  • If the user is already in the store, do not go to User Select unless it is time.

Making an HTTP request in disparate action creators looks like you will have more requests than you wanted. Do you need two levels of caching? One at the API level HTTP action (action creator) and one in stores? Doesn't that seem like overkill?

+3


source to share


2 answers


I would keep all this logic in the store. The key is to have separate action creators for fetch, get, and errors.

  • Call the staggered actions however appropriate. The store handles the fetch action by checking if it is present in the cache. If not, he makes a request.
  • The API response is pushed into a receive action. When this action is processed in the store, it adds it to the cache, but it needs to, and then fires the store change event.
  • If the answer is a mistake, push it into the creator of the error action so you can deal with it elsewhere.


If you have a chance of multiple fetch attempts before the response comes back, you can push the placeholder into the cache like Mika does.

0


source


we have been doing caching and lazy loading in stores. userStore.getUser returns the cached user if available, otherwise either calls the api directly or calls the action creator to execute the api request



One thing we haven't decided yet, although this is the correct way to track these pending requests. Right now we just create a placeholder object in the store and then fill it as soon as we get the data, but we can't easily see if the request is pending or completed for a given object

0


source







All Articles