JavaScript literal Object order of execution wrong

I am learning now JavaScript object oriented and try to build an literal object from an DB class for HTML5 SQLite.

The problem is, that on one point some methods are not executed in the right order, what I need. Here is the class:

var DB = function(dbName, dbVersion, dbDescription, dbSize){
var dbConnection    = null;
var is_connected    = false;

var dbName          = dbName;
var dbVersion       = dbVersion;
var dbDescription   = dbDescription;
var dbSize          = dbSize;

var existingTables  = new Array();

var connect = function(){
    Debug.log('auto connect ...');
    try{
        if (!window.openDatabase) {
            Debug.log('SQLite not supported');
        }
        else{
            dbConnection = window.openDatabase(dbName, dbVersion, dbDescription, dbSize);
            is_connected = true;
        }
    }
    catch(e){
        if (e == INVALID_STATE_ERR) {
            // Version number mismatch.
            Debug.log("Invalid database version.");
        }
        else{
            Debug.log(e.message);
        }
        return;
    }
}();

var checkTables = function(){
    dbConnection.transaction(function (tx) {
        tx.executeSql('SELECT name FROM sqlite_master WHERE type="table"', [], function(tx, rs) {
            for( var i = 0; i < rs.rows.length; i++ ) {
                Debug.log(rs.rows.item(i).name);
                existingTables.push( rs.rows.item(i).name );
            }
          }, function (tx, err){
              Debug.log(err.message);
              return true;
        });
    });
}();

// public methods
return {
    isConnected : function(){
        return is_connected;
    },
    close : function(){
        // close the DB connection
    },
    tableExists : function(table){
        Debug.log('table: '+table);

        // existingTables == 0 - WHY?
        alert(existingTables.length);

    },
    tableCreate : function(table){
        switch(table){
            case 'foo':
                var cr_sql = 'CREATE TABLE foo (id unique, text)';
            break;
        }

        // create the missing table
        dbConnection.transaction(function (tx) {
            tx.executeSql(cr_sql, [], function(tx, rs) {
                    return true;
                }, function (tx, err){
                  Debug.log(err.message);
                  return true;
            });
        });
    },
    dbConnection : dbConnection
}
};

Execution:

var DBFactory = {
getConnectionforApp: function(){
    try{
        var db_instance = new DB('mydb', '1.0', 'DB Connection 1', 1024*1024);
        Debug.log('Connected to db: '+db_instance.isConnected());
        return db_instance;
    }catch(e){
        Debug.log(e.message);
    }
}

};

// the example
var dbObj = DBFactory.getConnectionforApp();

alert(dbObj.tableExists('foo'));

When I execute this code, then the public method tableExists gives me an alert, that the existingTables.length = 0 , but I am adding all the existing tables in this Array on the beginning from the object in the function: checkTables().

  1. Why is this array existingTables empty in the function tableExists?
  2. Why is the function tableExists executed before the function checkTables?

Is there an possibility to make some construct() function, where it is called as first on object creation before all other functions?

Answers


The solution is the usage of own callback functions.

Here an running example:

var Debug = function(){
var debug   = true;
var type    = 'console';

function timestamp() {
    var date = new Date();
    var year        = date.getFullYear();
    var month       = date.getMonth();
    var day         = date.getDate();
    var hour        = date.getHours();
    var minute      = date.getMinutes();
    var seconds     = date.getSeconds();
    var miliseconds = date.getMilliseconds();
    minute = (minute < 10) ? "0" + minute : minute
    seconds = (seconds < 10) ? "0" + seconds : seconds
    return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+seconds+':'+miliseconds;
}

return{
    log : function(message){
        if(true == debug){
            switch(type){
                case 'console':
                    console.log(timestamp()+': '+message);
                break;
                case 'alert':
                    alert(timestamp()+': '+message);
                break;
                case 'document':
                    document.write('<br />'+timestamp()+': '+message);
                break;
            }
        }
    }
}
}();

var DB = function(dbName, dbVersion, dbDescription, dbSize){
var dbConnection    = null;
var is_connected    = false;

var dbName          = dbName;
var dbVersion       = dbVersion;
var dbDescription   = dbDescription;
var dbSize          = dbSize;

var existingTables  = new Array();

var connect = function(){
    Debug.log('call connect');
    if (!window.openDatabase) {
        Debug.log('SQLite openDatabase not supported');
        return false;
    }
    else{
        dbConnection = window.openDatabase(dbName, dbVersion, dbDescription, dbSize);
        is_connected = true;
    }
}();

function tableExists(table, callback){
    Debug.log('call tableExists');
    dbConnection.transaction(function (tx) {
        tx.executeSql('SELECT name FROM sqlite_master WHERE type="table"', [], function(tx, rs) {
            for( var i = 0; i < rs.rows.length; i++ ) {
                Debug.log(rs.rows.item(i).name);
                existingTables.push( rs.rows.item(i).name );
            }
          }, function (tx, err){
              Debug.log(err.message);
              return true;
        });
    }, null, function(){
        Debug.log('sql query call done');
        callback(checkTable(table));
    });
};

function checkTable(table){
    Debug.log('call checkTable');
    for(var i = 0; i < existingTables.length; i++) {
        if(existingTables[i] === table) {
            return true;
        }
    }
    return false;
}

// public methods
return {
    dbConnection : dbConnection,
    tableExists : tableExists,
    isConnected : function(){
        return is_connected;
    },
    tableCreate : function(table, callback){
        switch(table){
            case 'foo':
                var cr_sql = 'CREATE TABLE foo (id unique, text)';
            break;
            case 'bar':
                var cr_sql = 'CREATE TABLE bar (id unique, text)';
            break;
        }
        // create the missing table
        dbConnection.transaction(function (tx) {
            tx.executeSql(cr_sql, [], function(tx, rs) {
                    return true;
                }, function (tx, err){
                    Debug.log(err.message);
                    return true;
            });
        });
    }
};
};

var DBFactory = {
getConnectionforApp: function(){
    try{
        var db_instance = new DB('mydb', '1.0', 'DB Connection 1', 1024*1024);
        Debug.log('Connected to db: '+db_instance.isConnected());
        return db_instance;
    }catch(e){
        Debug.log(e.message);
    }
}
};

// the example
var dbObj = DBFactory.getConnectionforApp();

var table_exists = dbObj.tableExists('foo', function(exists){
Debug.log('Table foo exists: '+exists);
});

var table_exists2 = dbObj.tableExists('bar', function(exists){
Debug.log('Table bar exists: '+exists);
});

Need Your Help

Creating/updating an xml file from a mysql database table with php

php xml

I have a javascript image gallery which is getting data from an XML file. Im in the process of building a backend function in order for someone else to easy update the gallery. All the data require...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.