Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Should I put logic into ActionCreators? #86

Closed
ronag opened this issue Oct 25, 2014 · 1 comment
Closed

Should I put logic into ActionCreators? #86

ronag opened this issue Oct 25, 2014 · 1 comment

Comments

@ronag
Copy link

ronag commented Oct 25, 2014

I have a scenario where an action is to be dispatched however I need to perform some logic dependent on the state of certain stores to determine what actions to dispatch. Since a store cannot dispatch while already dispatching I cannot create a store which handles this logic. Note that this logic is non trivial.

Basically I need to perform an action which says "Calculate and apply a route which brings this resource to that target". The route that the resource has to take depends on available resources provided by the different stores. So I need to query all the stores and perform some logic to figure out what route is the best one and then dispatch the corresponding actions to the stores to make it happen.

Should I put the logic in the ActionCreator? Although that does not quite follow the guidelines which explicitly state the ActionCreators should just be helper methods to create and dispatch the actions.

@ronag ronag changed the title Should I put logic into the ActionCreator? Should I put logic into the ActionCreators? Oct 25, 2014
@ronag ronag changed the title Should I put logic into the ActionCreators? Should I put logic into ActionCreators? Oct 25, 2014
@fisherwebdev
Copy link
Contributor

You can put logic in an ActionCreator, but often there is a better design.

"Calculate and apply a route which brings this resource to that target"

Is this in response to a user interaction with the UI?

If so, I would try to do as much logic as possible in the stores, perhaps utilizing Dispatcher.waitFor(), and pass those values down as props to the view. In an event handler, the view would only apply the user-driven variables against those precalculated values that were passed down, and then call the appropriate ActionCreator with the parameters it needs.

If this is in response to another action, I would back up and look at how I am handling that action. I would gather all the required information, again possibly with waitFor(), to "apply a route..." in response to that original action.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants