好吧,事實證明,我發現處理這個問題的最好方法是使用閉包。像這樣的(僞代碼):
getThingieName: handler(function() {
var $dialog;
$dialog = $('<div id="thingie-name-dialog" class="ui-widget"></div>').html("<p>Enter a name for this thingie</p>\n<input type=\"text\" id=\"dlg-thingie-name\" style=\"width: 80%\" />").dialog({
autoOpen: false
}, {
title: 'enter a name',
modal: true,
buttons: {
Add: function() {
var value = $('#dlg-thingie-name').val();
$(this).dialog('close');
$('#thingie-name-dialog').remove();
return handler(value); // <= closure to handle the onAdd
},
Cancel: function() {
$(this).dialog('close');
return $('#thingie-name-dialog').remove();
}
}
});
return $dialog.dialog('open');
}),
getConfirmation: function(message, handler) {
var $dialog;
$dialog = $('<div id="confirmation-dialog" class="ui-widget"></div>').html("<p>" + message + "</p>").dialog({
autoOpen: false
}, {
title: 'confirm overwrite',
modal: true,
buttons: {
Ok: function() {
$(this).dialog('close');
$('#confirmatio-dialog').remove();
return handler(true); // <= closure to handle onOk
},
Cancel: function() {
$(this).dialog('close');
$('#Thingie-name-dialog').remove();
return handler(false); // <= closure to handle onCancel
}
}
});
return $dialog.dialog('open');
}
// Calling sequence
Snippets.getSnippetName(function(value) {
if (value == null) return false;
if (localStorage.getItem(value)) {
getConfirmation("This thingie, " + value + ", already exists. Overwrite?", function(response) {
if (response) return localStorage.setItem(value, snippet);
});
} else {
localStorage.setItem(value, snippet);
}
}
這可能不是最優的代碼,但它通過在處理器中嵌入他們做出依賴於按下按鈕對話框的觸發。
簡單地銷燬一個對話框並重新創建對話是相對容易的。 '$('selector')。dialog('destroy')。dialog()'會做到這一點。這就是說,我甚至不知道爲什麼這個對話框是有狀態的問題。您可以隨時對其進行配置,例如爲個別呼叫顯示的標題和文本。 – 2012-07-20 00:38:35
有狀態性質是客戶端Web應用程序中的一個問題,可能需要顯示多個對話框 - 在我的情況下,將對話框視爲已打開對話框的子對話框。爲什麼這是一個問題,jQUI「記住」並重用了很多狀態,在某些情況下會創建重擊值和/或文本。我不能只銷毀對話框#1,因爲對話框#1的yes處理程序根據對話框#2的yes/no應答而行動。 – 2012-07-20 05:42:03