related questions(not working):
scope-troubles-in-javascript-...
Pass extra parameters to WebSQL callback function?
I have a 'log' object to capture a few success or error variables as the websql transaction loops through the queries. There is a loop in a loop that is cycling through the data input which is provided from the server in the form of objects in arrays in objects, basically tables then rows. This all works fine until the internal success callback of the sql-query (not the final transaction success.) is called. as you can see from the below i've tried to call a function builder function to capture the table name variable but it is undefined when the returned function is called. I have tried many ways but i either end up with undefined or the last tables entries only.
I have tried to simplify my code below to focus on the issue, some of the code may be messy.I understand why its not available due to asynchronicity but not how to get around it.
addServData = function(data){
var columns, colCount, rowCount, Q, Qmks, table,
rows, dataWatch = {success:{},error:{}};
/*var tableName; //<-- MOVED THIS INTO LOOP AS THOUGHT WAS THE PROBLEM*/
oDb.transaction(function(tx){
for(var key in data){
var tableName = key;
table = data[key];
rows = table.data;
columns = table.columns;
colCount = table.colLen;
rowCount = table.rowLen;
if(rowCount <= 0) continue;
...
Q = 'BUILD QUERY.... (?,?,?)';
for(var x = rows.length-1; x >=0; x--){
var $i = rows.length - (x+1);// <-- INVERT COUNTER
//sort row object to array in order of colums;
var row = rows[$i],
params = utils.sortObjtoArr(row, columns);
tx.executeSql(Q, params,
buildCallback(tblName),
function(tx,error){
console.log('error: ', tx,error);
dataWatch.error[tblName + '::' + params[0]] = error;
});
}
}
function buildCallback(tbl){
//console.log('buildcallback'+tblName, tx); //PRINTS CORRECT NAME;
return function(tx,success,tbl){
console.log('success: ', tx, success, 'tblN:',tbl);//TBL = UNDEFINED;
dataWatch.success[tbl + '::' + success.insertId] = success.rowsAffected;
dataWatch.added += parseInt(success.rowsAffected);
}
}
}, function(tx,error){
console.log(error, dataWatch);
},
function(tx,success){
console.log('success', dataWatch); //WORKS
});
}