如何将回调附加到附加到延迟对象的延迟对象上? [英] How do you attach callbacks to the deferred object that is attached to a deferred object?

查看:90
本文介绍了如何将回调附加到附加到延迟对象的延迟对象上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一连串的ajax请求,它们支持一系列级联的下拉选择列表.当您在第一个下拉列表中选择一个值时,将触发一个请求以填充第二个,完成后(下拉列表已填充),下一个请求将触发以填充第三个下拉列表,依此类推.

I have a chain of ajax requests that support a series of cascading drop down select lists. When you select a value in the 1st drop down, a request is fired to populate the 2nd, when that is complete (and the drop down filled), the next request fires to populate the 3rd drop down, and so on down the line.

这些请求链的形成方式有些变化,所以我希望使用jQuery Deferred对象组装请求.

There are some variations to the way these request chains are formed, so I was hoping to assemble the requests using the jQuery Deferred objects.

我知道如何将第二个请求链接到第一个请求,但是我看不到如何将第三个请求链接到第二个.

I see how I can chain the 2nd request to the first, but I don't see how I can chain the third request to the 2nd.

function Step1() { return $.ajax(<foo>);}
function Step2() { return $.ajax(<foo>);}
function Step3() { return $.ajax(<foo>);}

$(function() {
   Step1().then(Step2).then(Step3);
});

目的是在解析Step2时触发Step3,但.then(Step2)返回的延迟对象来自Step1,因此将Step3作为回调添加到Step1.

The intent is that Step3 is triggered when Step2 is resolved, but the deferred object returned by .then(Step2) is from Step1, so Step3 is added as a callback to Step1.

如果您看到此jsFiddle示例,我想我想做的事情就更清楚了. 编辑:此处是同一脚本,但在第二个调用中添加了延迟使其更加明显.

I think it is clearer what I'm trying to do if you please see this jsFiddle sample. Edit:Here is the same script with a delay added to the second call to make it more obvious.

推荐答案

我们很幸运,并且在时间轴上有一定的灵活性.我们最终使用了.pipe()链接添加到jQuery 1.6中的延迟对象.

We were fortunate and had some flexibility in the timeline. We ended up using the .pipe() chaining added to deferred objects in jQuery 1.6.

感谢大家的帮助!

这篇关于如何将回调附加到附加到延迟对象的延迟对象上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆