使用WebSQL回调(Phonegap)

3

我正在使用Phonegap和Sencha Touch编写一个应用程序。我的viewport.js文件中包含Sencha界面,databasesFunctions.js文件包含所有数据库和请求函数。

我想在viewport.js中调用这行代码:

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

这是简化后的函数:

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;}

我不知道如何获取我的函数的返回值。通常,在我的函数末尾有一个返回(在标准SQL中),但是在这里,结果传输到另一个函数。

1个回答

3
这段代码能够实现你所需的功能,因为WebSQL接口是异步的,无法“返回”值。
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');
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接