Anthony de la Hoz
Anthony de la Hoz

Reputation: 137

Callback with WebSQL (Phonegap)

I am programming an application with Phonegap & Sencha Touch. I have my viewport.js file with Sencha interface and databasesFunctions.js with all databases and requests functions.

I want to call this line in viewport.js:

if(launchRequest('SELECT * from items',nombreItems)) alert('there are items');

Here is the simplified function:

function launchRequest(requete,callback){
    var db = openDatabase('database', '1.0', 'database', 2 * 1024 * 1024);

    db.transaction(function (tx) {

        tx.executeSql(requete,[],
        function (tx, results) {        
            return callback(results.rows.length);
        });

    });
}
function nombreItems(num) {return num;}

I don't know how to get the return value of my function. Usually, I have a return at the end of my function (in standard SQL), but here, the results are transmitted to another function.

Upvotes: 3

Views: 3134

Answers (1)

Adam Marshall
Adam Marshall

Reputation: 6045

This code will do what you are asking, because the WebSQL interface is asynchronous you cant "return" values.

launchRequest('SELECT * from items',nombreItems);

function launchRequest(requete,callback){
    var db = openDatabase('database', '1.0', 'database', 2 * 1024 * 1024);

    db.transaction(function (tx) {

        tx.executeSql(requete,[],
        function (tx, results) {        
            callback(results.rows.length);
        });

    });
}
function nombreItems(num) {
    if(num){
        alert('there are items');
    }
}

Upvotes: 3

Related Questions