Cordova - JS - Build在改变JS文件后似乎无法工作

时间:2016-06-07 06:18:16

标签: javascript cordova

我正在使用cordova 6.2.0

目标平台是Android,SDK API等级:15 - 19,

这里是index.js文件的代码

var app = {
initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
    app.receivedEvent('deviceready');

},
// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
// var database = {
//   var myDB = window.sqlitePlugin.openDatabase(
//     {name: "mySQLite.db", location: 'default'});
//
//   myDB.transaction(function(transaction) {
//     transaction.executeSql('CREATE TABLE IF NOT EXISTS phonegap_pro (id integer primary key, title text, desc text)', [],
//     function(tx, result) {
//       alert("Table created successfully");
//     },
//     function(error) {
//       alert("Error occurred while creating the table.");
//       });
//   });
//
//   var title="sundaravel";
//   var desc="phonegap freelancer";
//   myDB.transaction(function(transaction) {
//     var executeQuery = "INSERT INTO phonegap_pro (title, desc) VALUES (?,?)";
//     transaction.executeSql(executeQuery, [title,desc],
//     function(tx, result) {
//       alert('Inserted');
//     },
//     function(error){
//       alert('Error occurred');
//     });
//   });
// }
// database.initialize();
};

app.initialize();

每当我使用注释代码或任何代码更改js文件时,构建都不起作用,总是显示连接到设备而没有做任何事情。

Stuck there.

我使用了教程中的代码,链接:http://phonegappro.com/tutorials/phonegap-sqlite-tutorial-with-example-apache-cordova/

这是索取的index.html文件:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

PS:我是科尔多瓦的新手,请告诉我如果我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    createDatabase();

    console.log('Received Event: ' + id);
}

我创建了用于数据库条目的单独函数,从receiveEvent函数调用它,并且还对createDatabase()函数进行了一些更改,如下面的定义,现在工作正常,测试到我自己的cordova应用程序,所以请看看并让我知道你是否需要改变。

function createDatabase()
{
    try{
        var myDB = window.sqlitePlugin.openDatabase({ name: "mySQLite.db", location: 'default'});

        myDB.transaction(function(transaction) {
            transaction.executeSql('CREATE TABLE IF NOT EXISTS phonegap_pro (id integer primary key, title text, desc text)', [],
            function(tx, result) {
                alert("Table created successfully");
            },
            function(error) {
                alert("Error occurred while creating the table.");
            });
        });

        var title="sundaravel";
        var desc="phonegap freelancer";

        myDB.transaction(function(transaction) {
            var executeQuery = "INSERT INTO phonegap_pro (title, desc) VALUES (?,?)";
            transaction.executeSql(executeQuery, [title,desc],
            function(tx, result) {
                alert('Inserted');
            },
            function(error){
                alert('Error occurred');
            });
        });
    }
    catch(e)
    {
        alert(e);
    }
}