Flux calling actions with arguments managed in store
Asked Answered
P

3

7

Say I have an action someAction(params) that takes params which is managed in a store paramsStore:

paramsStore.listen(function(params) { 
  someAction(params)
}) 

It seems that I can't just call this in my view because apparently this goes against the Flux way of doing things (actions shouldn't be called within store listeners).

The reason I have someAction inside the store listener, is because I want it to be called every time the paramsStore is modified. How can I achieved this without resorting to the 'unpattern' of calling actions within stores listener?

Pemberton answered 1/12, 2015 at 21:11 Comment(0)
M
1

The right "flux way" of doing it would be to call the someAction(params) wherever information is dispatched to paramsStore.

Understanding what someAction does will give more clarity. Does it really need to be an action? If you're just doing some manipulation in the store data, you could have it as a local method in the paramStore.

flux architecture

Mulder answered 3/3, 2016 at 14:18 Comment(0)
W
0

While I am new to flux as well I could offer a suggestion. State that is needed to determine the outcome of an action that is held by Store A could be attached to a get method. This state can be retrieved by a View with a getter. When the action is called this state can be sent as a parameter. If something needs to be async it can now be done here (ajax call or something else) based on what the state is. Either the result of this or a promise object can then be used to trigger an action which is passed to the dispatcher. The dispatcher sends the result or promise to the store. The store then updates its state and the process repeats as necessary (when initial action is triggered).

I think a little more detail of what exactly you need would help actually. I do believe listening for for an action and triggering another action inside the store doesn't coincide with flux. I do think there is likely a way to accomplish the actual result you want using flux but without more detail this is the best I could come up with. Also, in reality you can implement anything you want. Flux is just a model and by extension a self imposed constraint to help with structure.

Wylde answered 1/12, 2015 at 22:58 Comment(0)
H
0

If you are using Flux as is, you could refer to the original image of the whole architecture at https://github.com/facebook/flux.

As you can see not only views could create actions. There are also Web API Utils which could create ones. Generally speaking not only API utils can do this. It's totally okey to create actions in order to start some behaviour according to outside world, some services or something else.

But in your case you are trying to create an action on some store update listener. As far as I can understand this would result in some changes in one or few other stores. In this case you probably don't need to create an action in the listener, but rather create some relations between your stores with waitFor API. Here is a link with detailed information: http://facebook.github.io/flux/docs/todo-list.html#adding-dependency-management-to-the-dispatcher.

Honkytonk answered 1/3, 2016 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.