尝试在设备就绪时执行代码

Trying to execute code on device ready

我正在尝试使用侦听器在 deviceready 上执行一些 Javascript 代码。

它似乎没有调用代码 - 我在控制台中没有得到任何信息,none 个变量已设置。

这是我正在使用的代码示例:

    <script>

                // Cordova is ready
function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({name: "my.db"});

  db.transaction(function(tx) {
    tx.executeSql('DROP TABLE IF EXISTS test_table');
    tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

    tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
      console.log("insertId: " + res.insertId + " -- probably 1");
      console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

      tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
        console.log("res.rows.length: " + res.rows.length + " -- should be 1");
        console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
      });

    }, function(e) {
      console.log("ERROR: " + e.message);
    });
  });
}
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);

</script>

现在,我主要不是 Javascript 开发人员(所以这个问题可能很简单),但是代码不执行有什么原因吗?我必须在匿名函数中 运行 它吗?

如何执行 onDeviceReady() 函数?

deviceready 是一个特殊的 Cordova 事件;除非您在 Cordova 环境中启动您的代码,否则该事件将永远不会被触发(您的代码也永远不会被执行)。

您可以可以 运行浏览器中的Cordova通过添加浏览器作为平台。有关详细信息,请参阅 http://www.raymondcamden.com/2014/09/24/Browser-as-a-platform-for-your-PhoneGapCordova-apps

您可以使用类似 Ripple 的东西来模拟 Chrome 中的 Cordova 环境。

由于Cordova-SQLitePluginAPI是异步的,你应该在数据库available/created之后执行事务。尝试以类似下面的方式实现它:

    var openDb = function (name, ok, error) {
      try {
        // SQLitePlugin always uses callbacks to specify the status of 'open' operation
        var dbRef = window.sqlitePlugin.openDatabase({name: name},
          function (db) {
            log("DataBase " + name + " opened!");
            ok(db);
          }, function (e) {
            try {
              dbRef.close();
            } catch (e) {
              log("Could not close database", e);
            }
            error(e);
          });
      } catch (e) {
        log("Could not open " + name + " DataBase");
        error(e);
      }
    };

    openDb("my.db", function(db){
       db.transaction(function(tx) {
        tx.executeSql('DROP TABLE IF EXISTS test_table');
        tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

        tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
          console.log("insertId: " + res.insertId + " -- probably 1");
          console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

          tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
            console.log("res.rows.length: " + res.rows.length + " -- should be 1");
            console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
          });

        }, function(e) {
          console.log("ERROR: " + e.message);
        });
      });
    });