Apologies but I seem to be going round in circles.
The process is fairly staightforward
-
SaveButtonClicked
-
call function to perform offline db transaction to update a record
-
check if online ( /*used the Ed Norton example for doing this */ )
-
if online call select transaction to fetch all updated records
-
success of select load results into an array
-
call webservice with array
so far I’ve tried to control the flow with
$.Deferred, dothis = defer.pipe(…),
.queue [but these seem to based on an element rather than a function completing] ,
putting the functions into an array ,
in testing the console always writes the ‘array built’ before the ‘building array’
var arrvals = [];
var deferA , deferB;
function updatedb(params){
db.transaction('update....',[params],updateOk,updateFail)
}
function updateOk(){
deferA.resolve()
}
function updateFail(){
deferA.reject()
}
function areweonline(){
$.ajax(.....)
}
function selectrows(){
db.transaction('update....',[params],buildarray,selectFail)
}
function buildarray(transaction,results){
console.log('building array')
for(i=0;i<=results.rows.length;i++){
var row = results.rows.item(i);
var job = {};
job.text = row["testtext"]
arrvals.push(job);
}
deferB.resolve()
}
function selectFail(){
deferB.reject();
}
function callwebservice(vals){
$.ajax(....) /* this bit is working fine*/
}
function SaveButtonClicked(){
deferA = $.Deferred();
deferB = $.Deferred();
$.when(deferA).then(
console.log('update completed')
$.when(deferB).then(function(){
console.log('array built')
callwebservice(arrvals)
})
)
}
So what would be the best approach? $.Deffered, queue, callback …dark magic?
try in this way
Some thoughts
push()onselectrowsfunction (I think it’s not what you want, becausearrvalsis empty);arrvalsis now defined inselectrows()function, and you can pass it when you resolve the deferred task;areweonline()function was removed from my example (since you’re not using it anywhere in your snippet);when()are now looking for promises (and not for deferred).Note: I didn’t tried to execute the code, but hope this helps anyway