javascript - How to know all JSON object has been iterated? -
this question has answer here:
- wait until jquery ajax requests done? 18 answers
i m working on phonegap product using jquery , jquery mobile, scenario is, when user logging in first time, sync data , after done forward user view. items retrieved in json format server. here portion of code. have called webservice , response returned json objects in response
variable.
$(response.data.module.registration).each(function(k,v){ //insert app's sqlite database }); $(response.data.module.attendance).each(function(k,v){ //insert app's sqlite database }); $(response.data.items.grocery).each(function(k,v){ //insert app's sqlite database }); //and on. there many rows in each loop above.
my question how know rows has been inserted loop can forward user restricted area.
more precisely, how know json object has been iterated successfully?
what tried put counter in each loop , check if sum of counters equal total items iterating. didn't work sum of counters readily available before items inserted.
edit
here helper function inserts record sqlite db. didn't work, user logged in before data inserted. can tell me went wrong
var sqlhelper = { insertjsondata:function(tablename,data,callback){ var dfrd = $.deferred(); var fields=sqlhelper.separatefielddata(data,"field"); var dataval=sqlhelper.separatefielddata(data,"value"); sqlhelper.db.transaction(function(tx) { var sqlquery='insert '+tablename+' ('+fields+') values('+dataval+')'; console.log(sqlquery); tx.executesql(sqlquery,[],function(tx,result){ dfrd.resolve(result); console.log('success'); },sqlhelper.errorcb); if(callback!=undefined){ callback(); } }); return dfrd.promise(); } }
and here code fetches server response
function defobj1() { if(typeof response.data.module.registration!="undefined"){ $(response.data.module.registration).each(function(i,e){ var data = { 'reg_id': e.reg_id, 'reg_name': e.reg_name, 'reg_desc': e.reg_desc, 'reg_status': e.reg_status }; sqlhelper.insertjsondata('tbl_registration',data); }); // end of each loop } } function defobj2() { if(typeof response.data.items.grocery!="undefined"){ $(response.data.items.grocery).each(function(i,e){ var data = { 'grocery_id': e.grocery_id, 'item_name': e.item_name, 'item_qty': e.item_qty, 'item_unit_price': e.item_unit_price }; sqlhelper.insertjsondata('tbl_grocery',data); }); // end of each loop } } $.when(defobj1() ,defobj2()).done(function(a1,a2){ //sync complete login user dologin(); })
thanks
try this. (edited)
var isvalid = true, = 0, sum, callback = function () { //if inserting called };
...
$(response.data.module.registration).each(function (k, v) { //insert app's sqlite database var data = { 'reg_id': e.reg_id, 'reg_name': e.reg_name, 'reg_desc': e.reg_desc, 'reg_status': e.reg_status }; sqlhelper.insertjsondata('tbl_registration', data, function (data) { if (!data) { isvalid = false; sum++; } i++;//onsuccess function checklast(i);//call lastly method or each }, function () { i++;//onerror function }); }); ... //other codes identical logic
...
function checklast(i) { if (i == sum) { callback(); } }
...
i have added successcallbak
, errorcallback
sqlhelper
var sqlhelper = { insertjsondata: function (tablename, data, successcallbak, errorcallback) { var dfrd = $.deferred(); var fields = sqlhelper.separatefielddata(data, "field"); var dataval = sqlhelper.separatefielddata(data, "value"); sqlhelper.db.transaction(function (tx) { var sqlquery = 'insert ' + tablename + ' (' + fields + ') values(' + dataval + ')'; console.log(sqlquery); tx.executesql(sqlquery, [], function (tx, result) { dfrd.resolve(result); if (successcallback) { successcallback(result); } console.log('success'); }, sqlhelper.errorcb); if (errorcallback) { errorcallback(); } }); return dfrd.promise(); } }
Comments
Post a Comment