在JQuery.Deferred对象之间传播事件 [英] Propagating events between JQuery.Deferred objects

查看:88
本文介绍了在JQuery.Deferred对象之间传播事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个JQuery.Deferred()对象上的所有事件传播到另一个对象?

How can I propagate all events on one JQuery.Deferred() object to another ?

是否有比调用其他对象.rejectWith.resolveWith处理.fail .then .done更好/更简单/更短的方法? 我假设我不必处理 .done .then,因为在我调用拒绝或解决时会调用它.

Is there a better / simpler / shorter way then handling both .fail and .then .done by calling the other objects .rejectWith and .resolveWith ? I am assuming that I don't have to handle .done .then as it will be called when I call Reject or resolve.

我怀疑也许可以使用.pipe来完成,但是我不确定如何.

I suspect that maybe this can be done using .pipe but I am not sure how.

更新:

注意:我还根据@FrédéricHamidi的评论修正了上面的一些文本.

Note: I also I fixed some of the text above per @Frédéric Hamidi's comment.

我要实现的目标如下:

我有一个API,它正在向服务器进行ajax调用, 他们将返回ajax对象(这是一个$ .Deferred本身).

I had a API that was making ajax calls to the server, they would return the ajax object (which is a $.Deferred itself).

在某些时候,我需要处理没有网络可用的情况(这是针对在移动设备上运行的phonegap应用程序的.)

At some point I needed to handle a situation where I had not net work available (This is for a phonegap application running on a mobile device).

我想做的是,在没有网络连接的情况下,我将显示一个对话框,提示用户修复网络问题并单击重试".

What I wanted to do was that in case of no network connectivity I would display a dialog prompting the user to fix the network issue and hit "retry".

但是在这种情况下,在用户单击重试之前,我不会调用$ .ajax,因此我没有$ .Defferred对象返回以API开头的函数.

But in this scenario I would not call the $.ajax until the user hits retry so I did not have a $.Defferred object to return the the function that called on the API to begin with.

所以我最终理解我可以创建一个新的$ .Defferred并返回它,并在重试按钮的回调中调用$ .ajax本身将其设置为中继"/将延迟的事件传播给我只是通过将其作为封闭的一部分进行钻孔而返回.

so I ended up understanding that I could just create a new $.Defferred and return it and on the callback for the retry button call the $.ajax itself setting it to "relay"/propagate the defferred events to the one I just returned by holing it as part of a closure.

但是我不确定这样做的方式.我将.pipe方法与之混用是因为它的描述说它是a:

But I was not sure of the way to do it. My mixup with the the .pipe method is because its description says its a:

实用程序方法来过滤和/或延迟链.

我希望这使我的问题更加清楚.

I hoep this makes my Question more clear.

推荐答案

如果我正确理解,您只是希望Deferred解析/拒绝并以与ajax请求完全相同的值进行通知?如果是这样,假设您的Deferred对象称为defer,则下面的代码可以解决问题:

If I understand correctly, you just want a Deferred to resolve/reject and notify with the exact same value as your ajax request? If so, assuming your Deferred object is called defer, the following code will do the trick:

$.ajax( ... ).done( defer.resolve ).fail( defer.reject ).progress( defer.notify );

之所以起作用,是因为defer.resolve,defer.reject和defer.notify在词法上是绑定的:上下文(this)和值(arguments)正确传递,因此您只是将ajax promise的状态有效地转发到了新的deferred中.

It works because defer.resolve, defer.reject and defer.notify are lexically bound: the context (this) and values (arguments) are passed properly so you just effectively forwarded the state of the ajax promise into the new deferred.

这篇关于在JQuery.Deferred对象之间传播事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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