What is the best way to structure those jquery call backs?

Posted by user518138 on Stack Overflow See other posts from Stack Overflow or by user518138
Published on 2012-06-03T04:19:03Z Indexed on 2012/06/03 4:40 UTC
Read the original article Hit count: 108

Filed under:
|

I am new to jquery and recently have been amazed how powerful those call backs are. But here I got some logic and I am not sure what the best way is. Basically, I got a bunch of tables in web sql database in chrome. I will need to go through say table A, table B and table C. I need to submit each row of the table to server. Each row represents a bunch of complicated logic and data url and they have to be submitted in the order of A -> B -> C.

The regular java way would be:

TableA.SubmitToServer()
{
    query table A;
    foreach(row in tableA.rows)
    {
       int nRetID = submitToServer(row);
       do other updates....
    }
}

Similar for tableB and table C.

then just call:

TableA.SubmitToServer();
TableB.SubmitToServer();
TableC.SubmitToServer();
//that is very clear and easy. 

But in JQuery, it probably will be:

db.Transaction(function (tx){
    var strQuery = "select * from TableA";
    tx.executeSql(strQuery, [], function (tx, result){
           for(i = 0 ; i < result.rows.length; i++)
           {
             submitTableARowToServer(tx, result.rows.getItem(i), function (tx, result)     {
                    //do some other related updates based on this row from tableA
                    //also need to upload some related files to server...
                });
           }
     }, 
     function errorcallback....)
});

As you can see, there are already enough nested callbacks. Now, where should I put the process for TableB and tableC? They all need to have similar logic and they can only be called after everything is done from TableA. So, What is the best way to do this in jquery?

Thanks

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about callback