将动作发送到Ember.StateManager:goToState是强制性的吗? [英] Sending action to Ember.StateManager : is goToState mandatory?

查看:30
本文介绍了将动作发送到Ember.StateManager:goToState是强制性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ember.StateManager的文档中,它说:在操作方法内,给定状态应委派goToState在其StateManager上的调用".这是否意味着如果我发送一个动作消息,我一定需要转换到另一个状态.是否可以保持相同的状态,但通过发送操作来完成某些任务?例如,我处于正在加载"状态,并且运行两个操作预处理"和显示".

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".

推荐答案

一般而言,在处理状态图时,您可以做任何想做的事情.在事件处理程序中切换状态不是强制性的.一个常见的情况是显示取消/保存对话框的事件处理程序.您可以将对话框轻松地放在事件处理程序的页面上,然后根据所按下的按钮进行相应的操作.

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.

一个单独的问题是应该每个事件处理程序基本上只是进入另一个状态.在上述情况下,您当然可以进入确认"状态,state-enter方法将显示对话框,并且将有两个处理程序,每个按钮一个.那些处理程序将转而前往其他州.

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

  • 在事件处理程序中,进入某种"MyDataSection"状态
  • 初始子状态为"loadData"
  • 输入"loadData"的状态方法开始加载过程
  • "loadData"中的事件处理程序"dataLoaded",用于在数据加载时进行处理;这意味着您需要在数据加载时触发一个事件
  • "dataLoaded"事件进入显示"状态
  • 显示状态显示视图(或删除活动指示器等)并处理来自显示的任何事件.

这里的好处是,如果您有多种方式进入应用程序的这一部分,则导致该部分的所有操作都只需进入此状态,并且所有事情都将始终发生.还要注意,由于视图事件处理程序处于显示状态,因此如果用户在加载数据时按下按钮,则不会发生任何事情.

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.

这篇关于将动作发送到Ember.StateManager:goToState是强制性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆