PhoneGap确认回调功能

时间:2013-05-30 13:03:29

标签: javascript mobile cordova

我目前正在使用jQuery Mobile和PhoneGap 2.0.0开发移动应用程序。 该应用程序是一个html文件,具有不同的div元素,代表应用程序的屏幕,本机容器用于使用javascript代码显示屏幕。

对于多个操作,我一直在使用本机警报并确认功能与用户进行交互。 例如:

function customBeforeMenuItemClick(screen, menuItem) {

if (screen === "APPROVALQUEUE" && menuItem === "Close") {
    return (confirm('Do you want to close without submitting changes?'));
}
}

此处该函数从确认弹出窗口返回并在其他函数(我无法控制)中使用它来处理关闭或不关闭应用程序的操作。

现在我需要修改弹出窗口的标题,并考虑使用Phonegap函数navigator.notification.alert并确认。但是,这些函数是异步的,并且脚本的执行仍在继续。

我试图使用回调方法返回正确的布尔值,但使用确认弹出窗口的函数已经完成执行

function onConfirmClose(button){
return (button == 1);
}

function toClose(){
navigator.notification.confirm("Close?", onConfirmClose,
 'Do you want to close', 'yes,no'); 
}

function customBeforeMenuItemClick(screen, menuItem) {

if (screen === "Start" && menuItem === "Cancel") {
    toClose();
}
else {return true;}
}

在此示例代码中,当函数toClose()完成后关闭应用程序时,弹出窗口会在消失之前短暂闪烁。

我尝试使用回调函数来设置一个全局变量,该变量将用于返回弹出窗口的结果,但它不起作用。

有没有办法获得navigation.notification.confirm的结果?

1 个答案:

答案 0 :(得分:0)

你可以这样关闭应用程序:

// you may alert the callback button to make sure which button sends which index
var YES_BUTTON = 1; // assuming that the 'YES' button will send the index 1

function onConfirmClose(button){
    // alert(button); ?
    if (button == YES_BUTTON) {
        navigator.app.exitApp(); // close the app
    }
}

如果仍然失败,您可以查看API Documentation,然后从测试示例开始。