我正在尋找一個好方法有時暫停一個動作(函數/方法調用),直到用戶確認他想要執行該動作的特定部分。我需要在不允許執行代碼的環境中執行此操作(本例中爲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>
和<ending-phase>
被複制。另外,因爲他們使用在<preliminary-phase>
中創建的對象,我不能將所有數據傳遞到這些函數,而是將它們移動到外部函數。
我目前的解決方案是,我封閉每個<optional-phase>
和<ending-phase>
在某些局部功能(讓他們有機會獲得在<preliminary-phase>
數據)宣佈之前,我要求確認我調用這些函數,而不是複製的代碼,但代碼不再以執行的順序看起來不正確。
你們會推薦什麼?
備註:
1. askForConfirmation
是一個非阻塞函數。這意味着緊跟其調用的代碼會立即執行(這就是爲什麼我的方法中有一個return;
)。
命令模式的+1。我會研究它。 – 2010-11-10 11:30:36