Skip to content

Instantly share code, notes, and snippets.

@dancek
Created January 28, 2013 14:24
Show Gist options
  • Save dancek/4655893 to your computer and use it in GitHub Desktop.
Save dancek/4655893 to your computer and use it in GitHub Desktop.
Reusable jQuery UI Dialog that creates a Deferred and returns a promise.
<div id="confirmation" title="Confirm action" style="display: none;">
<p>Are you sure?</p>
</div>
<script>
/* Reusable dialog with deferreds
*
* Example:
* $.modalConfirmation()
* .then(function() { console.log('success'); })
* .fail(function() { console.log('failure'); })
*/
var modalConfirmation = (function() {
var $dialog = $('#confirmation').dialog({
autoOpen: false,
modal: true
});
var showDialog = function() {
var def = $.Deferred();
$dialog.dialog('option', 'buttons',
{
"OK": function() {
def.resolve();
$(this).dialog('close');
},
"Cancel": function() {
def.reject();
$(this).dialog('close');
}
});
$dialog.dialog('open');
return def.promise();
};
return showDialog;
})();
</script>
@dancek
Copy link
Author

dancek commented Jan 29, 2013

Idea from http://stackoverflow.com/a/10703097/659526 plus the general idea that dialogs should be reused.

@captainkovalsky
Copy link

How to make general function which will work with different dialogs and custom buttons ? See my question
http://codereview.stackexchange.com/questions/52252/how-to-generalize-method-which-use-deferreds

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