我的应用程序由复杂的数据库体系结构支持,外部密钥限制很多,导致我的JavaScript充斥着callback
处理程序。对于单个操作,例如创建新订单,可能需要多达三个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
功能?
感谢您的投入。