Logic inside actions
From Flux Overview, we have these statements:
- Stores contain the application state and logic.
- The actions are simply discrete, semantic helper functions that facilitate passing data to the dispatcher.
That said:
https://github.com/facebook/flux/blob/master/examples/flux-chat/js/actions/ChatMessageActionCreators.js
var message = MessageStore.getCreatedMessageData(text);
ChatWebAPIUtils.createMessage(message);
I think that this logic should be placed inside the MessageStore:
https://github.com/facebook/flux/blob/master/examples/flux-chat/js/stores/MessageStore.js
case ActionTypes.CREATE_MESSAGE:
var message = MessageStore.getCreatedMessageData(action.text);
_messages[message.id] = message;
/* HERE we call an async request, but don't wait for a response
ChatWebAPIUtils.createMessage would trigger an error/success action */
ChatWebAPIUtils.createMessage(message);
MessageStore.emitChange();
break;
Business logic is not responsibility of actions. Actions are just simple semantic helpers, while stores contain the application state and logic.
What do you think about?
Yes, this is how I would actually build it, but we were trying to show that Web API calls can happen directly out of the action creators. There is nothing wrong with making the call in either place, per se, and sometimes its actually cleaner to do it in the action creators than to do it in the stores.
But the important part is what you have in the comment:
ChatWebAPIUtils.createMessage would trigger an error/success action
I'm open to changing the example, but I do want to keep it as simple as possible.
Hey @fisherwebdev, thank you for your perfect answer.
I get the idea. But looking at the Flux repo, I saw an updated flowchart that makes this clear:
Action creators should be used to call web API, not stores.
This "rule" makes it simple to know what part of the code is calling the web API. As you said, it's actually cleaner to do it in the action creators.
Thank you!