Skip to content

Instantly share code, notes, and snippets.

@joeldart
Created July 27, 2011 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeldart/1110291 to your computer and use it in GitHub Desktop.
Save joeldart/1110291 to your computer and use it in GitHub Desktop.
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));
@joeldart
Copy link
Author

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