提示用户在流程中间进行确认

我正在寻找一种有时暂停动作(函数/方法调用)的好方法,直到用户确认他想要执行该动作的特定部分。我需要在不允许代码执行停止的环境中执行此操作(在我的情况下是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;
)。     
已邀请:
注意:我不是100%确定我得到你的具体情况。 命令模式可能适用于此处。它与人们的建议相似。 您有一系列按顺序执行的命令。
[<preliminary-phase>, <mandatory-phase>, <optional-phase>, <ending-phase>]
只需将命令从阵列中移出一个并调用execute方法即可。 在可选阶段,检查是否需要用户确认,如果没有,则执行调度命令完成事件的可选代码方法,如果需要则显示警报,等待事件,检查结果调度命令完成事件或调用可选方法(将运行然后调度命令完成)。 您还可以创建一个命令树,这样就可以清楚地说明执行流程,而不必弄乱阵列。 这就是安装向导等程序的工作原理。 它的优点在于执行顺序很好且可见,并且您的代码很好地分解为块,并且封装了每个步骤的复杂性。例如,可选阶段对结束阶段一无所知。可选阶段只知道用户在执行之前可能需要提示,并且它在内部处理所有这些。 http://en.wikipedia.org/wiki/Command_pattern   “使用命令对象可以更容易地构造需要在选择时委托,排序或执行方法调用的通用组件......”     
“代码不再按照它执行的顺序”实际上对我来说似乎很好。如果代码没有按照它执行的顺序写入,只要它清楚就可以了。事实上,由于你的代码以变量顺序执行,我认为你不可能按照它将执行的顺序编写而不重复代码,这是一个更大的罪恶。选择好的功能名称,您的方法将通过我的代码审查。
<preliminary-phase>

<mandatory-phase>

var optional_phase = function() {
  <optional-phase>
}

var ending_phase = function() {
  <ending-phase>
}

if(<user-confirmation-is-needed>){
    askForConfirmation(function(){
        if(<user-response-is-positive>)
            optional_phase();
        ending_phase();
    });
    return;
}

optional_phase();
ending_phase();
    
这是否符合您的要求?
<preliminary-phase>

<mandatory-phase>

if(<user-confirmation-is-needed>){
    askForConfirmation(function(){
        if(<user-response-is-positive>)
            <optional-phase-as-local-function>
        <ending-phase-as-local-function>
    });
} else {
    <optional-phase-as-local-function>
    <ending-phase-as-local-function>
}
    
不是一个巨大的变化,但如果这个流程工作,可选阶段不会重复
<preliminary-phase>

<mandatory-phase>

if(<user-confirmation-is-needed>){
    askForConfirmation(function(){
      if(<user-response-is-negative>)
      {
        <ending-phase>
        return;
      }
  });
}

<optional-phase>
<ending-phase>
    

要回复问题请先登录注册