Domanda

I want to call a callback function using javascript, as you can see from this example code from http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#database_size

function queryDB(tx) {
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

function querySuccess(tx, results) {
    console.log("Returned rows = " + results.rows.length);
    // this will be true since it was a select statement and so rowsAffected was 0
    if (!results.rowsAffected) {
        console.log('No rows affected!');
        return false;
    }
    // for an insert statement, this property will return the ID of the last inserted row
    console.log("Last inserted row ID = " + results.insertId);
}

function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB, errorCB);

The method "queryDB" is called without any parameters, but the function is defined to receive the variable "tx". Coming from PHP, I can't understand how this works, because it does. Also there is querySuccess which receives 2 parameters and also works.

My problem is that I need to send 1 my own parameter, so I have modified the call for queryDB like this:

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB(myParam), errorCB);

and

function queryDB(tx, myParam) {
    tx.executeSql('SELECT * FROM' + myParam, [], querySuccess, errorCB);
}

But is does not work.

1) How come there are unsent params being received? 2) How can I add my own params without breaking it?

È stato utile?

Soluzione 3

Ok, so I think I have found the answer. Reading from here http://theelitist.net/passing-additional-arguments-to-a-callback-function-in-javascript I have realised that Dawson Loudon's response was almost complete, but he missed the "tx" variable that should be passed on the first function that calls the 2nd one, that would be:

db.transaction(function(tx) { queryDB(tx, myParam); }, errorCB);

function queryDB(tx, myParam) {
    //logic here
}

Thanks for your time and help.

Altri suggerimenti

Try this:

db.transaction(function() { queryDB(tx, myParam); }, errorCB);

function queryDB(tx, myParam) {
    //logic here
}

The transaction function is looking for callback methods which do not include parameters. You can 'hijack' this by assigning an anonymous function that calls the function you want with parameters.

When you write

db.transaction (queryDB, errorCB);

you are passing as a parameter the function itself, but if you try this:

db.transaction (queryDB(myParam), errorCB);

you'd be passing as a parameter the result of the function, which will generate an error.

db.transacción receives parameter are 2 functions, If you need to pass custom parameter simply write them in the declaration

function queryDB(tx, myParam) {
    //Using your myParam
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
...
db.transaction(queryDB, errorCB);

Remember that javascript is an interpreted programming language and is not object-oriented

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top