简体   繁体   中英

Sending action to Ember.StateManager : is goToState mandatory?

In the documentation of Ember.StateManager it's said that : "Inside of an action method the given state should delegate goToState calls on its StateManager". Does it mean that if I send an action message, I necessarily need to transit to another state. Is it possible to stay in the same state but doing some task by sending an action ? For example, I'm in a state "loading" and I run two actions "preprocess" and "display".

Very simply: an action message may but does not have to transition to another state.

Something you didn't ask, but is related and important: it is a bad idea and bad design to call goToState in an enter or exit method.

When dealing with statecharts in general, you can do whatever you want. It's not mandatory to switch states in an event handler. A common case would be an event handler that shows a cancel/save dialog. You can easily put the dialog on the page in the event handler, and proceed accordingly depending on which button is pressed.

A separate question is should every event handler basically just go to another state. In the above scenario, you can certainly go to a "confirm" state, the state-enter method will show the dialog, and there would be two handlers, one for each button. Those handler would in turn go to other states.

Both design choices I think are equally valid, at least in that scenario. If you choose to implement a separate state for every action, you will end up with a lot of small but concise states. If you choose to do stuff in the event handlers themselves, your states will be bigger, but there will be less of them.

One thing I will say is if an event handler is getting complicated, you are probably better of with a new state. Also, be consistent.

For you specific scenario, if I'm reading it right, you want to load data and then change the display to show the data, based on an event. In this case, I would use new states.

So you press a button that starts the process

  • In the event handler, go to some sort of 'MyDataSection' state
  • Initial substate is 'loadData'
  • Enter state method of 'loadData' starts the loading process
  • Event handler 'dataLoaded' in 'loadData' to handle when the data loads; this means you need to fire an event when the data loads
  • 'dataLoaded' event goes to the 'show' state
  • show state shows the view (or removes activity indicator etc) and handles any events that come from the display.

What's good here is that if you have multiple ways to get to this section of the app, all actions that lead to this section only need to go to this state, and everything will always happen the same. Also note that since the view event handlers are on the show state, if the user hits a button while the data is loading, nothing will happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM