Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
showDialog deferreds example
$(function () {
$("#deleteAll").click(function () {
$.showDialog("You will lose all your data if you continue.")
.then(function (result) {
if (result) {
$.showDialog("Losing your data is permanent and cannot be restored. Are you sure?", "Loss is permanent", "Yes", "No")
.then(function (result) {
if (result) {
alert("all data lost!");
}
});
}
});
});
});
(function ($, undefined) {
$.showDialog = function (msg, title, okTxt, cancelTxt) {
title = title || "Warning";
okTxt = okTxt || "OK";
cancelTxt = cancelTxt || "Cancel";
var dfd = $.Deferred(),
$dialog = $("<div></div>")
.html(msg)
.dialog({
autoOpen: false,
title: title,
modal: true,
buttons: [
{
text: okTxt,
click: function () {
dfd.resolve(true);
$(this).dialog("close");
}
},
{
text: cancelTxt,
click: function () {
dfd.resolve(false);
$(this).dialog("close");
}
}
],
close: function (event, ui) {
if (!dfd.isResolved()) {
dfd.reject();
}
}
});
$dialog.dialog("open");
return dfd;
};
}(jQuery));
Owner

joeldart commented Jul 27, 2011

Example of using jQuery deferreds by implementing showDialog similar to the old MessageBox.Show in winforms development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment