javascript - PhoneGap确认回调函数

标签 javascript mobile cordova

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

对于几个操作,我一直使用 native 警报和确认功能与用户交互。 例如:

function customBeforeMenuItemClick(screen, menuItem) {

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

这里的函数从确认弹出窗口获取返回值,并将其用于其他函数(我无法控制)来处理关闭或不关闭应用程序的操作。

现在我需要修改弹出窗口的标题,并想到使用Phonegap函数navigator.notification.alert和confirm。但是,这些函数是异步的,脚本的执行会继续。

我尝试使用回调方法返回正确的 bool 值,但使用确认弹出窗口的函数已经执行完毕

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的结果?

最佳答案

您可以像这样关闭应用程序:

// 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然后从示例开始进行测试。

关于javascript - PhoneGap确认回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16836984/

相关文章:

javascript - 如何在没有装饰器的情况下重写这个js类

android - 我无法为 Eclipse 安装 PhoneGap 插件

cordova - 如何在 Cordova 中正确定义 Content-Security-Policy?

javascript - 如何将javascript数组发送到服务器?

javascript - ctrl+c "content"来自 CSS

javascript - Google Cloud 上的 Ajax 到 Python

listview - Flutter有状态代码问题。设置什么状态?

javascript - 强制地址栏显示在移动版 Chrome 应用中

jQuery Mobile 忽略表单元素上的数据角色 ="none"

ios - 如何在 PhoneGap/Cordova 2.0 中使用预填充的 SQLite 数据库?