简体   繁体   English

如何使用jQuery / UI的Dialog阻止对话(“打开”)调用

[英]How do I block a dialog(“open”) call with jQuery/UI's Dialog

I'm trying to create a function that shows a modal dialog which when called blocks until the dialog is closed, this will allow for a result to be returned to the caller 我正在尝试创建一个显示模式对话框的函数,该对话框在调用块时直到对话框关闭,这将允许将结果返回给调用者

The following function is an attempt which has two problems. 以下功能是一个有两个问题的尝试。

  1. It returns the result while the dialog is still open. 它在对话框仍处于打开状态时返回结果。
  2. The selector test does not find the dialog, inspecting with firebug reveals that the id element is lost once the dialog is created. 选择器测试找不到对话框,使用firebug检查显示创建对话框后id元素丢失。

.

function getCountrySelection() {
    var ctryCode;
    var dlg = $("#JS-field-dlg-ctry-select");

    if (dlg.size() === 0) {
        dlg = $("<div id='JS-field-dlg-ctry-select' title='Select Country' class='dialog-fields'></div>");
        dlg.append("Customer found in both Australia and New Zealand");
        dlg.dialog({
            autoOpen: false,
            width: 400,
            height: 160,
            modal: true,

            buttons: {
                "Australia": function() {
                    ctryCode = "au";
                    $(this).dialog("close");
                },
                "New Zealand": function() {
                    ctryCode = "nz";
                    $(this).dialog("close");
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }
        });
    }

    dlg.dialog('open');

    return ctryCode;
}

EDIT : I thought I'd show how I'm calling this: 编辑 :我想我会告诉我如何称呼它:

buttons: {
    "Find": function() {
        var custAu = JS.sales.getCustomer("au", inpCust.val());
        var custNz = JS.sales.getCustomer("nz", inpCust.val());

        var cust;
        if (custAu === undefined && custNz === undefined) {
            alert('No customer could be found with that number.');
            return;
        } else if (custAu !== undefined && custNz !== undefined) {
            var ctry;
            getCountrySelection(function(result) {
                ct = result;
            });
            if (ctry === "au") {
                cust = custAu;
            } else if (ctry === "nz") {
                cust = custNz;
            } else {
                return;
            }
        } else if (custNz === undefined) {
            cust = custAu;
        } else {
            cust = custNz;
        }

        if (cust) {
            $(this).dialog("close");
            // Do something with cust.
        } else {
            alert('Customer could not be found.');
        }
    },
    "Cancel": function() {
        $(this).dialog("close");
    }
}

There is no way to block execution until the dialog closes; 在对话框关闭之前无法阻止执行; JavaScript does not allow to "suspend" execution. JavaScript不允许“暂停”执行。 Your best bet is to change the contract of your function; 你最好的办法是改变你的职能合同; instead of returning the value straight away, it should accept a callback function that it will call with the result as soon as the dialog is dismissed. 它不应该立即返回值,而应该接受一个回调函数,一旦对话框被解除,它将调用结果。 Then the code calling this will provide a suitable callback in which it can continue its execution. 然后调用它的代码将提供一个合适的回调,它可以继续执行它。

Something like this: 像这样的东西:

function getCountrySelection(callback) {
(...)
            buttons: {
                "Australia": function() {
                    $(this).dialog("close");
                    callback("au");
                },
                "New Zealand": function() {
                    $(this).dialog("close");
                    callback("nz");
                },
                "Cancel": function() {
                    $(this).dialog("close");
                    callback();
                }
            }
        });
    }
    dlg.dialog('open');
}

Then use: 然后使用:

getCountrySelection(function(result) {
    if (result) {
        ...handle result...
    } else {
        ...the user cancelled the dialog...
    }
});

This is basically the same thing as with AJAX calls; 这与AJAX调用基本相同; you can't "suspend" your AJAX call to wait until the AJAX actually completes and returns the result, hence "asynchronous". 你不能“暂停”你的AJAX调用,等到AJAX实际完成并返回结果,因此“异步”。

EDIT: In your specific example, you could use it like this: 编辑:在您的具体示例中,您可以像这样使用它:

buttons: {
    "Find": function() {
        var custAu = JS.sales.getCustomer("au", inpCust.val());
        var custNz = JS.sales.getCustomer("nz", inpCust.val());

        if (custAu === undefined && custNz === undefined) {
            alert('No customer could be found with that number.');
            return;
        } else if (custAu !== undefined && custNz !== undefined) {
            getCountrySelection(function(ctry) {
                var cust;
                if (ctry === "au") {
                    cust = custAu;
                } else if (ctry === "nz") {
                    cust = custNz;
                } 
                handleCustomer(cust);
            });
        } else if (custNz === undefined) {
            handleCustomer(custAu);
        } else {
            handleCustomer(custNz);
        }

        function handleCustomer(cust) {
            if (cust) {
                $(this).dialog("close");
                // Do something with cust.
            } else {
                alert('Customer could not be found.');
            }            
        }
    },
    "Cancel": function() {
        $(this).dialog("close");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM