我的應用程序是由一個複雜的數據庫架構,其導致我的JavaScript是充滿了callback
處理外鍵約束沉重的支持。對於單一行爲,例如創建新訂單,可能會有多達三個XHR需要提交所有必要的數據進行處理。ExtJS的4.1 - 處理順序XHR的不使用同步請求
目前,我在我的感覺是一個相當不適度或至少是的,有點草率手段處理這...
/** -- PSEUDO CODE -- */
myOrder.save({
success: function()
{
/** This request handled inserting the main attributes of the model
into the database, which means it is safe to fire off additional
requests */
myOrderContents.store.sync({
success: function()
{
/** Possibly fire another request here, that is dependent on
the first two requests returning successfully, or maybe
execute code that is also dependent on these requests.
*/
}
});
},
failure: function()
{
/** Handle Failure Here */
}
});
我意識到另一個可行的辦法是沿東西線調用一個回調方法,那麼這將只執行的代碼的某一大塊....所有請求
myOrder.save({callback: executeAfterRequests});
myOrderContents.store.sync({callback: executeAfterRequests});
myOtherObject.save({callback: executeAfterRequests});
executeAfterRequests: function()
{
if(requestA.isComplete() && requestB.isComplete() && requestC.isComplete())
{
/** Execute some code after all requests have been completed... */
}
}
鑑於ExtJS的開發的強烈反對同步請求,這裏是我最好的選擇來擴展基類AJAX並實施隊列?或者,接受AJAX的本質,並繼續以我的快樂方式嵌套success
的功能?
謝謝您的輸入。
展望未來,實施承諾/ A +將是在一個非常同步狀的方式處理異步的理想手段。看起來Promise的目標是早在ES7中內置到DOM中,這將非常棒! –