简体   繁体   English

Cordova SQLite插件仅在首次调用时有效

[英]Cordova SQLite plugin only works on first call

I have a cordova app with two pages, the first has a search field and the second displays the results of the first. 我有一个带有两页的cordova应用程序,第一页有一个搜索字段,第二页显示了第一页的结果。 The displayResults function displayResults函数

function displayResults(){
    var query = localStorage.getItem('search');
    var db = window.sqlitePlugin.openDatabase({name:"Store-DB", location: 1});
    queryDB(db,query);
    }

function queryDB(db,query){
    db.transaction(function(tx) {
            var queryList = query.split(" ");
            var sqlText = "SELECT * FROM DB WHERE item LIKE '%"+queryList[0]+"%'";
            for (i = 1; i < queryList.length; i++) {
                    sqlText += " OR item LIKE '%"+queryList[i]+"%'";
            }
            tx.executeSql(sqlText, [], querySuccess);
        }
        );
    }

function querySuccess(tx,results) {
    var i;
    var len = results.rows.length;
    //Iterate through the results
    for (i = 0; i < len; i++) {
        console.log(row);
        //Get the current row
        var row = results.rows.item(i);
        var div = document.createElement("div");
        div.style.background = "gray";
        div.style.color = "white";
        div.innerHTML = row;

        document.body.appendChild(div);
    }
    alert("Finished");
}

Here is the code for the second page: 这是第二页的代码:

    <link rel="stylesheet" type="text/css" href="css/default.css">
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script src="js/zepto.js" type="text/javascript"></script>
    <script src="plugins/com.brodysoft.sqlitePlugin/www/SQLitePlugin.js" type="text/javascript"></script>
    <script src="js/code.js" type="text/javascript"></script>
    <script type="text/javascript">
        function onLoad(){
            document.addEventListener("deviceready", onDeviceReady(), false);
        }
        function onDeviceReady(){
            displayResults();
        }
    </script>

    </head>
    <body onload="onLoad()">
        <div id="taskbar">
            <a href="index.html">Home</a>
            <a id="username" class="toolbar_item" style="float:right;"  href="#"></a>
        </div>

The first time you load this page displayResults works just fine, but if you click the link back to the main page and load the second page again the console prints the error ReferenceError: no "undefined" has no property "openDatabase" in other words, the SQLite plugin isn't loading. 第一次加载此页面时, displayResults可以正常工作,但是如果您单击返回首页的链接并再次加载第二个页面,则控制台将显示错误ReferenceError: no "undefined" has no property "openDatabase" ,换句话说, SQLite插件未加载。 But, if I make displayResults fire on a button press instead of onload it works every time. 但是,如果我使displayResults在按钮按下时触发,而不是onload则每次都可以使用。 What is the explanation for this peculiar behavior? 这种特殊行为的解释是什么?

It seems that sometimes deviceready-event is either not fired at all or fired too early when there are more event handlers of different libraries in place. 似乎有时当没有更多的不同库的事件处理程序时,deviceready-event要么根本不触发,要么触发得太早。 The second problem caused that the brodysoft-plugin is not loaded properly and assign to the window-object as window.sqlitePlugin property because one event handler dispatched deviceready-event too early. 第二个问题导致brodysoft插件未正确加载,并以window.sqlitePlugin属性的形式分配给窗口对象,因为一个事件处理程序过早调度deviceready-event。 It turned out setTimeout was the answer as it had been here 原来setTimeout是这里的答案

You can use the onpageshow event for example 您可以使用onpageshow事件为例

$('#idofyourpage').bind("onpageshow",function(){
   //Do something
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM