I'm currently working on a prototype mobile app that should support pulling data from a server to the device, storing that data in an SQLite DB on the device, allow the user to manipulate the data, then push the data from the SQLite DB back up to the server. So far, everything is working great until the last part. I'm having trouble getting information back out of the SQLite tables and into a JSON object to push back up to the server.
My basic issue at this point is figuring out how to build a JSON object from the data in SQLite given that all db transactions in javascript are asynchronous from my understanding.
I've got a jsFiddle with some example code: http://jsfiddle.net/uu29g/
My hope is to push something up to the server that looks like this:
[{"order_num": "2001", "custnum": "ABC123", "custname": "Demo Customer", "status": "S", "id": 1, "lineitems": [{"id": 1, "line_num": "1", "part_num": "WSC599", "part_desc": 'Widget A', "qty": "2.0", "price": "15.00"}, {"id": 2, "line_num": "2", "part_num": "FFC898B", "part_desc": 'Dongle B', "qty": "1.0", "price": "32.98"},
{"id": 3, "line_num": "3", "part_num": "RQFG", "part_desc": 'Dooflatchie', "qty": "5.0", "price": "200"}]},{"order_num": "2002", "custnum": "ZXY987", "custname": "Another Customer", "status": "S", "id": 2, "lineitems": [{"id": 1, "line_num": "1", "part_num": "GHJH11", "part_desc": 'Lightsaber - Green', "qty": "1.0", "price": "1500.00"},
{"id": 2, "line_num": "2", "part_num": "C3P0", "part_desc": 'Protocol Droid', "qty": "1.0", "price": "9811.04"}]}]
Given the database in the jsFiddle example, what would be the best way to generate an object like the above from the SQLite data? Edit: Specifically, how do I build that object from three async queries? My current train of thought is to get a list of order numbers from the order_header table (in my jsFiddle example), loop over those order numbers and pull the order_header info and order_detail/line item info from the tables for each order and push that into an object, then push each order object into a final array/object. Where I'm struggling is with how to get the header and detail data from SQLite when the sql queries are asynchronous. (And I might be going about this totally wrong).
*FYI: I'm still learning about Deferred objects in jQuery. The app I'm working on is built using jQuery Mobile, and meant to work on iOS/Android and will be turned into a native app via phoneGap