【问题标题】:phonegap jquerymobile - load a page when asynchronous stuff is donephonegap jquerymobile - 完成异步内容后加载页面
【发布时间】:2012-12-05 00:59:50
【问题描述】:

我想加载一个外部页面(因此不使用查询移动设备中的 ajax),问题是当我使用通过单击链接标签激活的功能时,并且在此功能中我有一个 INSERT 到数据库中,该链接在脚本写入数据库之前被跟踪。

这里是部分代码:

$('#aggiungiClienteRubrica').click(function() {

                               db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
                               db.transaction(function(tx){
                                              var sql = 'INSERT INTO CLIENTI (nome, cognome) VALUES ("'+$('#nome').val()+'", "'+$('#cognome').val()+'")';
                                              tx.executeSql(sql)}, errorCB);
                               });

在 html 文件中我只有一个这样的标签:

<a id="aggiungiClienteRubrica" href="../client/consultClients.html" data-role="button" data-theme="b" rel="external">Add client</a>

所以问题是,我可以执行 javascript,但是异步调用将插入到 db 中的记录没有被执行并且 ../client/consultClients.html 页面被加载

我怎样才能让它在工作完成后跟随链接??

【问题讨论】:

    标签: jquery cordova jquery-mobile asynchronous


    【解决方案1】:

    试试这个:

    
    $('#aggiungiClienteRubrica').click(function(e) {
        e.preventDefault(); // will stop default link action
    
        // capture the link url
        var url = $(this).attr('href');
    
        // your DB stuff here, wait for completion
        // (I'm not familiar with local DB so don't know that part)
    
        // use this if you managed to do the above synchronously
        // or in a 'success' callback
        window.location.href = url;
    });
    
    

    【讨论】:

    • 嗯,链接已被关注,但数据库未更新,我正在尝试其他类似主题中建议的一些类似的东西,但还没有让它工作......
    • 哇,我刚刚发现了一个全新的世界,关闭.. 我需要研究它,因为 1 小时前我什至不知道它存在.. 顺便说一句,如果有人愿意将一些代码作为关闭示例将不胜感激!
    • 我自己不看文档我不知道如何为 executeSql 调用传递回调,但我更新了我的代码以显示如何捕获链接 href。您可以在回调中使用 url 变量。请注意,您不能使用 $(this),因为在您的闭包中范围会有所不同。
    【解决方案2】:

    好的,成功了!!!

    方法如下:

    $('#aggiungiClienteRubrica').click(function(e) {
                                   var myVar = click(e);
                                   myVar;
    });
    

    那么点击函数是这样定义的:

    function click(e){
    
    db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
    e.preventDefault();
    db.transaction(function(tx){
                   var sql = 'INSERT INTO CLIENTI (nome, cognome) VALUES ("'+$('#nome').val()+'", "'+$('#cognome').val()+'")';
                   tx.executeSql(sql)}, errorCB, successCreation);
    }
    

    然后,定义回调函数successCreation:

    function successCreation(){
    var href = $('#aggiungiClienteRubrica').attr('href');
    window.location.href = href;
    
    }
    

    蒂姆感谢您的支持...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多