提示用户在流程中间进行确认
我正在寻找一种有时暂停动作(函数/方法调用)的好方法,直到用户确认他想要执行该动作的特定部分。我需要在不允许代码执行停止的环境中执行此操作(在我的情况下是ActionScript,但JavaScript的方法应该相同)。
为了说明,这是在引入用户提示之前的动作模型:
<preliminary-phase> // this contains data needed by all the following phases //
<mandatory-phase> // this will be always be executed //
<optional-phase> // this will always execute too, if in this form, but in some cases we need to ask the user if he wants to do it //
<ending-phase> // also mandatory //
我需要的是插入一个条件用户提示,“你想做这个部分吗?”,并且仅在用户想要的时候做<optional-phase>
。
<preliminary-phase>
<mandatory-phase>
if(<user-confirmation-is-needed> and not <user-response-is-positive>){
<do-nothing>
}
else{
<optional-phase>
}
<ending-phase>
在ActionScript / JavaScript中尝试执行此操作时,我得到了以下内容:
<preliminary-phase>
<mandatory-phase>
if(<user-confirmation-is-needed>){
askForConfirmation(callback = function(){
if(<user-response-is-positive>)
<optional-phase>
<ending-phase>
});
return;
}
<optional-phase>
<ending-phase>
现在<optional-phase>
和ѭ5都是重复的。另外,因为它们使用在ѭ6中创建的对象,所以在不将所有数据传递给这些函数的情况下,我无法将它们移动到外部函数。
我目前的解决方案是,在我要求确认之前,我在一些本地函数中包含了<optional-phase>
和ѭ5each(以便它们可以访问<preliminary-phase>
中的数据),我调用这些函数而不是复制代码,但似乎没有正确的是,代码不再按照执行顺序排列。
你们会推荐什么?
笔记:
1.askForConfirmation
是一种非阻塞功能。这意味着紧跟其调用的代码会立即执行(这就是为什么我的方法中有一个return;
)。
没有找到相关结果
已邀请:
4 个回复
犯痪桂涛杭
只需将命令从阵列中移出一个并调用execute方法即可。 在可选阶段,检查是否需要用户确认,如果没有,则执行调度命令完成事件的可选代码方法,如果需要则显示警报,等待事件,检查结果调度命令完成事件或调用可选方法(将运行然后调度命令完成)。 您还可以创建一个命令树,这样就可以清楚地说明执行流程,而不必弄乱阵列。 这就是安装向导等程序的工作原理。 它的优点在于执行顺序很好且可见,并且您的代码很好地分解为块,并且封装了每个步骤的复杂性。例如,可选阶段对结束阶段一无所知。可选阶段只知道用户在执行之前可能需要提示,并且它在内部处理所有这些。 http://en.wikipedia.org/wiki/Command_pattern “使用命令对象可以更容易地构造需要在选择时委托,排序或执行方法调用的通用组件......”
倪蕊悲潍
吠强祷豪硅
丧泉缝锋