Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 581
Anthony de la Hoz Avatar asked Dec 14 '25 16:12

Anthony de la Hoz


1 Answers

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');
    }
}
like image 128
Adam Marshall Avatar answered Dec 17 '25 04:12

Adam Marshall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!