jQuery同步确认对话框

时间:2017-11-16 08:32:21

标签: jquery twitter-bootstrap event-handling confirm

我使用jQuery + Bootstrap + jqGrid插件来处理网格。 当在网格中进入编辑模式时,可以告诉该插件退出编辑模式(并保存更改)或保持编辑模式(不节省)。 现在的目的是只能通过确认对话框关闭此编辑模式。 不幸的是,这似乎很难实现,因为事件是异步的......

简单示例(未经确认):

//this works: either true or false wille be returned...
var saveRow = true;
//...
$grid.on("beforesaverow", function(){
    if (saveRow){
        return true; //OK: row will be saved ... exiting edit mode
    }
    else{
        return false; //NOT OK: row will not be saved ... continue edit mode
    }
});

以下情况不起作用:

//this won't not work: true will always be returned!
var saveRow = true;
//...
$grid.on("beforesaverow", function(){
    confirmDialog("Save row?", function(answer){ //...open a simple bootstrap dialog with question yes/no...
        //...entering callback...
        if (answer){
            saveRow = true;
        }
        else{
            saveRow = false;
        }
    });
    return saveRow; //NOT OK: saveRow will always be "true"... function is ended but confirm is still busy...
});

现在应该如何实施:

var saveRow = true;
//...
$grid.on("beforesaverow", function(){
    confirmDialog("Save row?", function(answer){ //...open a simple bootstrap dialog with question yes/no...
        //...entering callback...
        if (answer){
            saveRow = true;
            //obviously this is not correct because we are NOT returning "true" from "beforesaverow"...
            return true; //alternative needed...
        }
        else{
            saveRow = false;
        //obviously this is not correct because we are NOT returning "false" from "beforesaverow"...
        return false; //alternative needed...
        }
    });
});

这只是一个例子......我认为使用这种结构可以在任何地方出现这个问题...

什么可行,但不是很好的编码,使用普通的旧javascript:

$grid.on("beforesaverow", function(){
    var answer = window.confirm("Save row?"); //...open the browser's dialog with question ok/cancel...
    if (answer){
        saveRow = true;
    }
    else{
        saveRow = false;
    }
    return saveRow; //perfect! either true or false will be returned!
});

浏览器的确认对话框就像一个警告:程序流将暂停,直到用户关闭对话框......但这不是很好的练习......

@stackoverflow我已经找到了一些关于延迟对象的东西,但这也不行。 问题似乎是儿童功能应该返回他的父功能我猜...

另一个澄清jqGrid不是问题的例子:

$elem.on("click", function(e){
    var stopEventBubbling = false;
    confirmDialog("cancel this click event?", function(answer){
        stopEventBubbling = answer;
    }
    return stopEventBubbling; // the value of "stopEventBubbling" will never be the returned "answer" form the confirm dialog!
});

0 个答案:

没有答案