Reputation: 137
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
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