简体   繁体   中英

Cordova - JS - Build doesn't seem to work after changing the JS file

I am using cordova 6.2.0

Targeted platform is Android, SDK API Level: 15 - 19,

Here's the code for index.js file

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();

Whenever I change the js file with the Commented code or any code the build is not working, always showing connecting to device and not doing anything.

Stuck there.

I used the code from a tutorial, link: http://phonegappro.com/tutorials/phonegap-sqlite-tutorial-with-example-apache-cordova/

Here's the index.html file as requested:

<!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: I am new to Cordova, please let me know If I am doing something wrong here.

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);
}

I have created seperate function for database entry calling it from receivedEvent function and also made some changes into createDatabase() function as define below, and this is working fine now, tested into my own cordova app so please have a look and let me know if you require any change.

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);
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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