向 WebSQL 回调函数传递额外的参数?

发布于 2024-12-04 01:23:50 字数 679 浏览 3 评论 0原文

我使用以下代码调用函数 db.transaction:

    db.transaction(createSheetDB, function(){alert("Sheet creation error!")}, function(){alert("Sheet created!")});

函数 createSheetDB 是一个回调函数,由 db.transaction() 隐式调用,该函数还向其传递参数 tx。我已经实现了如下所示的函数 createSheetDB(tx):

function createSheetDB(tx) {
var nextId = getNextId();
tx.executeSql("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')", [], 
        function(){alert("Sheet row inserted!")}, 
        function(tx, err){alert("Sheet row insertion Error: "+err.message+" "+err.code)}
);}

现在的问题是sheetName 和desc 的值仅在调用函数中可用。如何将它们传递给函数 createSheetDB(tx)?

I am calling a function db.transaction with following code:

    db.transaction(createSheetDB, function(){alert("Sheet creation error!")}, function(){alert("Sheet created!")});

The function createSheetDB is a callback function which is implicitly called by db.transaction() which also passes it a parameter tx. I have implemented function createSheetDB(tx) like this:

function createSheetDB(tx) {
var nextId = getNextId();
tx.executeSql("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')", [], 
        function(){alert("Sheet row inserted!")}, 
        function(tx, err){alert("Sheet row insertion Error: "+err.message+" "+err.code)}
);}

Now the problem is the values of sheetName and desc are available only in the calling function. How do I pass them onto function createSheetDB(tx)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-12-11 01:23:50

您可以使用一种技术来创建一个新的回调来关闭您想要的变量。

function doStuff(callback) {
    var val = 43;
    callback(val);
}

function myCallback(val, anotherVal) {
    alert("val: " + val + "\nanotherVal: " + anotherVal);
}

(function() {

    var anotherVal = "Whoa!",
        anotherCallback = function(val) {
            return myCallback(val, anotherVal);
        };

    doStuff(anotherCallback);

}());​

You can use a technique whereby you create a new callback that will close over the variables you want.

function doStuff(callback) {
    var val = 43;
    callback(val);
}

function myCallback(val, anotherVal) {
    alert("val: " + val + "\nanotherVal: " + anotherVal);
}

(function() {

    var anotherVal = "Whoa!",
        anotherCallback = function(val) {
            return myCallback(val, anotherVal);
        };

    doStuff(anotherCallback);

}());​
淑女气质 2024-12-11 01:23:50

作为函数创建回调

function createSheetDB(sheetName, desc) {
    return function(tx){
        var nextId = 1;
        alert("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')");
        /* delete above two lines and uncomment for your code
        var nextId = getNextId();
        tx.executeSql("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')", [], 
            function(){alert("Sheet row inserted!")}, 
            function(tx, err){alert("Sheet row insertion Error: "+err.message+" "+err.code)}
        );
        */
    }
}

// dummy code to show as example
db = {transaction: function(fn,lose,win){return fn(),win();}};

function testIt(){
    var sheetName = 'hello',
        desc = 'world';
    db.transaction(createSheetDB(sheetName, desc), function(){alert("Sheet creation error!")}, function(){alert("Sheet created!")});
    // note how createSheetDB is now called with the vars you want
}

Callback creation as a function

function createSheetDB(sheetName, desc) {
    return function(tx){
        var nextId = 1;
        alert("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')");
        /* delete above two lines and uncomment for your code
        var nextId = getNextId();
        tx.executeSql("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')", [], 
            function(){alert("Sheet row inserted!")}, 
            function(tx, err){alert("Sheet row insertion Error: "+err.message+" "+err.code)}
        );
        */
    }
}

// dummy code to show as example
db = {transaction: function(fn,lose,win){return fn(),win();}};

function testIt(){
    var sheetName = 'hello',
        desc = 'world';
    db.transaction(createSheetDB(sheetName, desc), function(){alert("Sheet creation error!")}, function(){alert("Sheet created!")});
    // note how createSheetDB is now called with the vars you want
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文