I have a jq replacement of the standard confirm dialog.
I am allowing the user to pass in a function that will be called if the user clicks on ok.
The passed in function may call a long running async task like an ajax call, sometimes not, I want to cater for both.
I used this solution as a base (https://stackoverflow.com/a/19374933)
What I want to achieve is something like this:
MyConfirmation('A Title', 'A message to the user', function() {
$.get('/some/bigdata/longrunning');
}
).then(function() {
// add to dom here
});
This is MyConfirmation:
MyConfirmation = function (title, message, ok) {
var d = $.Deferred();
$('<div/>')
.text(message)
.dialog({
buttons: {
"Ok": function () {
if($.isFunction(ok)) {
ok.apply();
}
$(this).dialog("close");
d.resolve(true);
return true;
},
"Cancel": function () {
$(this).dialog("close");
d.resolve(false);
return false;
}
},
close: function(event, ui) {
$(this).remove();
},
resizable: false,
draggable: false,
width: 'auto',
title: title,
modal: true
});
return d.promise();
};
I want to ideally combine with my deferred which returns true or false to indicate if the user clicked ok or cancel.
I know I can use the .done method on the promise returned from $.get but how would I make my solution work