2014-02-14 187 views
-2

我有一個功能,推動來自其他功能,也解決承諾數組承諾。我只是想知道這個代碼是否可以。JQuery延遲解決其他延遲

這裏的主要功能是

/// <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)); 
} 

這裏是由上述功能

/// <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)); 
} 

而且所有上述方法是推動下面的遞延調用的函數。

/* 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()); 
} 

這是鏈條的聲音?還沒有測試,但我認爲這是確定的。只是想一些其他的眼睛在這之前,我繼續......

+3

這是不是有點太多問,只是檢查是否有人可能會發現一個問題,如果你甚至沒有自己測試它?! 爲什麼你不能自己發現這是否正常工作? – Martin

+1

@Martin,因爲當前正在開發的本地平板電腦數據庫中沒有加載數據。我的任務是編寫沒有數據的數據同步代碼。只是問一個問題。我不認爲這個問題對於任何理解延期的人來說都很難。所以提供一個答案或保持你的無意義的評論給自己... – gdex

+0

很積極... – Martin

回答

1

這確實應該在Code Review,事實並非如此,但在這裏不用...

幾個意見:

  • 書面,.processValidationResults()中的進度消息表示發出的請求,而不是收到的響應。因此它會直接跳到「計數」。

  • .processValidationResults(),else { promise.push($.when()); }是不必要的。

  • .updateInvalidChildren(),var def = $.Deferred()是不必要的。

  • jQuery Deferred's resolvereject方法是「可拆卸的」。在this.executeSql()中,您只需簡單地傳遞參數,就可以簡化tx.executeSql(...)調用tx.executeSql(sql, params, def.resolve, def.reject)

  • 假設self.db.executeSql執行AJAX(並且tx.executeSql()不會以某種方式對請求進行排隊),但代碼有可能在同時發生請求時很難打到服務器。確保它可以處理此代碼將生成的同時請求的數量。否則,採取措施使請求順序進行。

+0

感謝您提供您的意見,我會張貼在代碼審查現在。在你的第二個項目符號中,我想你的意思是說沒有必要推遲一個空的延期?因此,如果數組中沒有任何內容,那麼$ .when.apply將返回已解決的承諾? executeSql是SQLite的aync API的一部分,因此它不會僅僅更新設備上的本地SQLite數據庫而只是發出服務器請求,所以我認爲在那裏我確定。再次感謝您的意見 – gdex

+0

'$ .when()'和'$ .when.apply($,[])'是相同的語句。你的代碼已經(正確)假定'$。when()'返回已解決的承諾。因此,假定'$ .when.apply($,[])'也將返回一個已解決的承諾是完全安全的。在這裏證明:http://jsfiddle.net/SWU8D/。另一方面,我不確定你可以忽略SQL查詢超載的可能性。也許SQLite數據庫可以使用多個快速查詢,也許不會 - 我不知道。確保你的測試製度給你足夠的壓力讓你滿意。 –

+0

再次感謝!單元測試肯定會擊中SQLite插件。當前的數據庫設計對於任何給定的父實體具有少量(最多5個)相關子實體。我們並不期望在同步的這個特定部分進行大量的更新,但是這又取決於最終用戶同步數據的頻率,因此您的觀點是有效的。 – gdex