John Doe
John Doe

Reputation: 3243

How to execute a callback on JqueryUI dialog when the dialog is closed

I am using JueryUI dialog to display an alert message. The issue that is occuring for me is that callback is immediately being executed. I would expect the callback only to be executed once the dialog box is closed.

Is there a proper way to ensure the callback doesnt execute until the dialog is closed?

 myAlert = function (title, msg, callback) {
        var alertElement = $('#alertElement ');
            alertElement .dialog({
                modal: false,
                resizable: false,
                width: 500,
                buttons: {
                    OK: function () {
                        $(this).dialog("close");
                    }
                },
                open: function (event, ui) {
                    $(this).parent().find('.ui-dialog-titlebar-close').hide(); 
                    $(this).parent().find('.ui-dialog-buttonpane button:first').focus();
                },
                close: function () {
                    if (typeof callback == "function")
                    { callback(); }
                }
            });
        }
    };

This is my call...

var urlString = "some url string";
function printWindow(urlString) {            
    window.open(urlString, 'Print', "toolbar=no,menubar=no,status=no");
};

myAlert("Warning", "This is a warning.", printWindow(url));

Upvotes: 0

Views: 83

Answers (1)

T J
T J

Reputation: 43166

callback is immediately being executed because you are invoking it with (), so the return value of printWindow method will be passed to myAlert.

To pass printWindow to be called later with the url as an argument you can use bind method:

myAlert("Warning", "This is a warning.", printWindow.bind(null,url));

return value of the bind method is a new printWindow function whose first parameter will be the second argument (url) passed to bind.

Upvotes: 1

Related Questions