public
Created

showDialog deferreds example

  • Download Gist
exampleUse.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$(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!");
}
});
}
});
});
});
jQuery.showDialog.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
(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));

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.