Bootbox.confirm可以同步工作吗?

时间:2014-03-27 10:07:43

标签: c# javascript jquery asp.net bootbox

在aspx页面中,有一个像这样的asp:linkbutton:

<asp:LinkButton runat="server" ID="btExit" Text="Exit"
                OnClientClick="javascript:return confirmExit();" 
                EnableViewState="false" 
                OnClick="ExitButtonClick"></asp:LinkButton>

这是javascript函数:

<script type="text/javascript">
function confirmExit() {
    bootbox.confirm("Are you sure?", function (confirmed) {
        return confirmed;
    });
}
</script>

问题在于,据我所知,bootbox.confirm异步工作,并且代码隐藏的ExitButtonClick函数在不等待用户确认的情况下执行。

我找到了一个有效的解决方案,使用隐藏按钮:

<asp:LinkButton runat="server" ID="btExit" Text="Exit"></asp:LinkButton>
<asp:Button runat="server" ID="btExitHidden" onclick="ExitButtonClick" style="display:none;" />

这是javascript部分:

<script type="text/javascript">
    $("#btExit").click(function (e) {
        e.preventDefault();
        bootbox.confirm("Are you sure?", function (confirmed) {
            if (confirmed) {
                $("#btExitHidden").click();
            }
        });
    });
</script>

我的问题是,如果有一种更“美观”和“标准”的方式与Bootbox.confirm同步工作,而不使用隐藏按钮。

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式制作自定义同步引导箱功能:

function ayncBootbox(message, cb = null) { // cb : function
        return new Promise(resolve => {
            bootbox.confirm({
                message: message,
                buttons: {
                    confirm: {
                        label: "Yes"
                    },
                    cancel: {
                        label: "No"
                    }
                },
                callback: cb ? result => cb(resolve, result) : result => resolve(result)
            })
        })
}

如果你需要做一些额外的事情,你可以通过传递一个自定义回调来调用它

var result = await ayncBootbox("message", (resolve, result) => resolve(result))

或者只是

var result = await ayncBootbox("message")

PS:不要忘记将调用者函数也设为异步 :) 如果需要,您可以使用拒绝来扩展此代码

答案 1 :(得分:-2)

我的解决方案

@Html.ActionLink("Delete", "DeleteReport", new { id = item.Id }, new { @class = "btn btn-danger", onclick = String.Format("return ASE.ConfirmAction(this.href, 'Delete {0}?');", item.Name) })
var ASE = {
    ConfirmAction: function (href, text) {
        bootbox.confirm(text, function (result) {
            if (result)
                window.location = href;
        });

        return false;
    }
}
相关问题