Pass extra parameters to WebSQL callback function?

心不动则不痛 提交于 2019-12-05 08:32:49

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

}());​

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!