我的应用程序是由一个复杂的数据库架构,其导致我的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中,这将非常棒! –