Created
July 27, 2011 20:27
-
-
Save joeldart/1110291 to your computer and use it in GitHub Desktop.
showDialog deferreds example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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!"); | |
} | |
}); | |
} | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of using jQuery deferreds by implementing showDialog similar to the old MessageBox.Show in winforms development.