5

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

Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35
  • 1
    There are *many* questions/answers for the generic case (use a closure that binds to local variables then calls the real callback function or define the real callback function in the same lexical scope or ..), so hopefully this *refined case* will be more applicable .. but I don't use WebSQL. –  Sep 16 '12 at 01:44
  • thanks @pst, I am currently using this way to solve my problem. – Balkrishna Rawool Sep 19 '12 at 08:28

2 Answers2

3

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

}());​
Josh
  • 44,706
  • 7
  • 102
  • 124
1

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
}
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Thanks @shhac. This solution looks good. But to me the one from Josh looks cleaner. Although it could just be me. ;) – Balkrishna Rawool Sep 19 '12 at 08:32
  • That is fair enough. I would find Josh's way harder to follow the variables in, in a big piece of code which is why I keep them all together as just one function. – Paul S. Sep 19 '12 at 13:19