1

查看此 Phonegap 示例实现:

//Create database if not exist
var transaction = function (tx) {

tx.executeSql(query);


};

//error callback if any errors occured during a transaction
var errorCB = function (the_error) {

   console.log(the_error);
}

//success callback when database transaction successful
var successCB = function () {

   console.log("transaction success");
}


//initialise
function init() {

db = window.openDatabase("db", "1.0", "Database", 2000000);
db.transaction(transaction, errorCB, successCB);

};

如何向回调函数添加额外的参数?

我希望每个事务都有相同的回调并传递事务 id/name 以便识别它,而不必每次都编写不同的回调

我看到的所有示例都是针对单个事务的。传递给 errorCB的error对象不包含事务标识符或类似内容。如果我有多个db.transaction调用(每个调用都有一个或多个 tx.executeSql),我是否必须一直编写不同的回调?

我正在寻找类似的东西: db.transaction(transaction, errorCB(tx_name), successCB(tx_name));

谢谢

4

1 回答 1

3
db.transaction(transaction, 
    function(tx){errorCB(tx, tx_name);}, 
    function(tx){successCB(tx, tx_name);}
);

function errorCB(tx, tx_name) {
    ...
    ...
    ...
}

function successCB(tx, tx_name) {
    ...
    ...
    ...
}
于 2013-07-31T18:36:34.960 回答