好吧,事实证明,我发现处理这个问题的最好方法是使用闭包。像这样的(伪代码):
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