I have a function that pushes promises from other functions that are also resolving arrays of promises. I was just wondering if this code is OK.
Here is the main function
/// <summary>Process the validation results (invalid IDR records will have the iop=4)</summary>
this.processValidationResults = function ()
{
var promises = [];
var count = (self.idrValidationList.length - 1);
$.each(self.idrValidationList, function (index, idrValidationItem)
{
_onProgress(self.utils.formatCounterMessage(index, count, 'Processing Validation Items'));
if (idrValidationItem.is_valid = 0)
{
//there is a problem with this IDR record
//update the idr_insp table
promises.push(self.updateInvalidEntity(self.configEntities.idr_insp, idrValidationItem.idr_insp_id));
promises.push(self.updateInvalidChildren(self.configEntities.idr_insp, idrValidationItem.idr_insp_id));
}
else
{
//push resolved promise
promise.push($.when());
}
});
return ($.when.apply($, promises));
}
Here are the functions that are called by the above function
/// <summary>Update the invalid record, sets the IOP field to 4 [Cannot sync due to issue]</summary>
/// <param name="entity" type="Object">GLobal entity definiton </param>
/// <param name="tabletId" type="Int">Primary Key on the tablet to change</param>
this.updateInvalidEnity = function (entity, tabletId)
{
//update the record with the new ID and IOP status
var updateSql = 'UPDATE ' + entity.name + ' SET iop=? WHERE ' + entity.key_field + '=?';
//update the record
return (self.db.executeSql(updateSql, [4, tabletId]));
}
/// <summary>Update the invalid child records, sets the IOP field to 4 [Cannot sync due to issue]</summary>
/// <param name="entity" type="Object">GLobal entity definiton </param>
/// <param name="keyId" type="Int">Foreign Key on the tablet to change</param>
this.updateInvalidChildren= function (parentEntity, keyId)
{
var promises = [];
$.each(parentEntity.child_entities, function (index, child)
{
var def = new $.Deferred();
var updateSql = 'UPDATE ' + child.child_name + ' SET iop=? WHERE ' + child.key_field + '=?';
promises.push(self.db.executeSql(updateSql, [4, keyId]));
});
return ($.when.apply($, promises));
}
And all of the above methods are pushing the deferred below.
/* Executes the sql statement with the parameters provided and returns a deffered jquery object */
this.executeSql = function (sql, params)
{
params = params || [];
var def = new $.Deferred();
self.db.transaction(function (tx)
{
tx.executeSql(sql, params, function (itx, results)// On Success
{
// Resolve with the results and the transaction.
def.resolve(itx, results);
},
function (etx, err)// On Error
{
// Reject with the error and the transaction.
def.reject(etx, err);
});
});
return (def.promise());
}
Is this chain sound? Have not tested yet but I think it is OK. Just want some other eyes on this before I continue...