Created
January 28, 2013 14:24
-
-
Save dancek/4655893 to your computer and use it in GitHub Desktop.
Reusable jQuery UI Dialog that creates a Deferred and returns a promise.
This file contains hidden or 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
<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> |
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
Idea from http://stackoverflow.com/a/10703097/659526 plus the general idea that dialogs should be reused.