javascript - Angular deferred implementation only outputs last value of loop -
i have custom synchronization process queue up, in order, of sync records. when service retrieves more 1 sync record, process them, update last sync date every successful record, or log error when fails (without updating last sync date) , abort sync process.
i've implemented $q.all angularjs. here's subset of sync loop:
var processes = []; (var in data) { if (data[i] === null || data[i].tablename == null || data[i].query == null || data[i].params == null) { // let's throw error here... throw new typeerror("error! data retrieved download sync process of unexpected type."); } var params = data[i].params; var paramsmassaged = params.replaceall("[", "").replaceall("]", "").replaceall(", ", ",").replaceall("'", ""); var paramsarray = paramsmassaged.split(","); mlog.log("query: " + data[i].query); mlog.log("params: " + paramsarray); if (data[i].tablename === "table1") { var process = $table1_dbcontext.executesyncitem(data[i].query, paramsarray); process.then( function () { $dbconfigurations_dbcontext.updatelastsyncdate(data[i].createddate, function (response) { mlog.log(response); }); }, function (response) { mlog.logsync("error syncing record: " + response, "error", data[i].id); }, null ); processes.push(process); } else if (data[i].tablename === "table2") { var process = $table2_dbcontext.executesyncitem(data[i].query, paramsarray); process.then( function () { $dbconfigurations_dbcontext.updatelastsyncdate(data[i].createddate, function (response) { mlog.log(response); }); }, function (response) { mlog.logsync("error syncing record: " + response, "error", data[i].id); }, null ); processes.push(process); } else { mlog.logsync("warning! table not included in sync process. have outdated version of application. table: " + data[i].tablename); } } $q.all(processes) .then(function (result) { mlog.logsync("---finished syncing records"); }, function (response) { mlog.logsync("sync failure - " + response, "error"); });
example executesyncitem function:
executesyncitem: function (script, params) { window.logger.logit("in table1 executesyncitem function..."); var primarykey = params[params.length - 1]; var deferred = $q.defer(); $dbservice.executequery(script, params, function (insertid, rowsaffected, rows) { window.logger.logit("rowsaffected: " + rowsaffected.rowsaffected); if (rowsaffected.rowsaffected <= 1) { deferred.resolve(); } else { deferred.resolve(errormessage); } }, function (tx, error) { deferred.reject("failed sync table1 record primary key: " + primarykey + "; error: " + error.message); } ); return deferred.promise; }
the problem i'm running is, if there more 1 sync records fail, line displays same value records failed (not sure if it's first failure record, or last).
mlog.logsync("error syncing record: " + response, "error", data[i].id);
how display information specific record failed, instead of same message "x" times?
as mentioned comradburk
wrapping processes in closure within loop solution, there angular
way in solving problem. instead of using native for-in
loop, can via angular.foreach()
, loop through data
elements.
var processes = []; angular.foreach(data, function(item) { if (item === null || item.tablename == null || item.query == null || item.params == null) { // let's throw error here... throw new typeerror("error! data retrieved download sync process of unexpected type."); } var params = item.params; var paramsmassaged = params.replaceall("[", "").replaceall("]", "").replaceall(", ", ",").replaceall("'", ""); var paramsarray = paramsmassaged.split(","); mlog.log("query: " + item.query); mlog.log("params: " + paramsarray); if (item.tablename === "table1") { var process = $table1_dbcontext.executesyncitem(item.query, paramsarray); process.then( function () { $dbconfigurations_dbcontext.updatelastsyncdate(item.createddate, function (response) { mlog.log(response); }); }, function (response) { mlog.logsync("error syncing record: " + response, "error", item.id); }, null ); processes.push(process); } else if (item.tablename === "table2") { var process = $table2_dbcontext.executesyncitem(item.query, paramsarray); process.then( function () { $dbconfigurations_dbcontext.updatelastsyncdate(item.createddate, function (response) { mlog.log(response); }); }, function (response) { mlog.logsync("error syncing record: " + response, "error", item.id); }, null ); processes.push(process); } else { mlog.logsync("warning! table not included in sync process. have outdated version of application. table: " + item.tablename); } }); $q.all(processes) .then(function (result) { mlog.logsync("---finished syncing records"); }, function (response) { mlog.logsync("sync failure - " + response, "error"); });
Comments
Post a Comment