重用jQuery.post()/ jQuery.Deferred()对象对象、jQuery、post、Deferred

2023-09-11 01:01:20 作者:那個街角丶俄們牽手走過

最简单的我要找的例子是这样的:

the simplest example of what I'm looking for is this:

var messageLoader = $.post("api/user/messages", {api:data})

messageLoader.done(function(data){
   //do something 
});

这出色的作品,但只有一次。如果我想更新数据,我必须重新定义一切。

This works brilliantly, but only once. If I want to update the data, I have to redefine everything.

我似乎无法找到任何要求,它可以让我重新启动它的延迟的对象。即 messageLoader.redo(),在理想情况下会重新做POST请求,并随后调用相同的完成的处理程序,而无需重新定义它我。

I can't seem to find any call for the Deferred Object that let's me restart it. i.e. messageLoader.redo(), which ideally would re-do the POST request, and subsequently call the same "done" handler without me having to re-define it.

我可以把它所有的功能,只需再次调用这个函数,但是这不是我要找的,因为我还不想做到这一点:

I could put it all in a function and simply call that function again, but that's not what I'm looking for, because I would further want to do this:

var messageLoader = $.post("api/user/messages", {api:data})
var friendRequestLoader = $.post("api/user/friendrequests", {api:data})

$.when(messagesLoader, friendRequestLoader)
    .done(function (messages, friendRequests) {
        // update display of messages and friend requests
        // attach Handlers
    });

$("#updateMessages").click(function(){
    messageLoader.redo() // This doesn't exist
})

我们的想法是,点击 $(#updateMessages)将再次做到这一点要求,那么 $。(当)处理程序将使用新的 messageLoader 数据,和原来的 friendRequestLoader 数据。

The idea is that clicking $("#updateMessages") would re-do just that request, then the $.when() handler would use the new messageLoader data, and the original friendRequestLoader data.

我已经看了,在文档中这样的事情,但没有发现任何东西。也许这里有人知道它的存在,还是有办法来完成同样的事情。

I've looked in the docs for something like this but haven't found anything. Perhaps someone here knows if it exists, or a way to accomplish the same thing.

推荐答案

我是pretty的肯定只是不是为了工作,你想要的方式递延类,因为他们意在一次性使用对象。我甚至对在jQueryConf整个会议Deferreds坐在,并重新使用它们甚至没有提及。

I'm pretty sure the Deferred class just isn't designed to work the way you want, because they're intended to be one-time use objects. I even sat in on a whole session on Deferreds at jQueryConf, and re-using them wasn't even mentioned.

此外,你可以从递延API页面看到:

Furthermore, as you can see from the Deferred API page:

http://api.jquery.com/category/deferred-object/

有没有重新启动的方法。您的也许的能够手动更改的延迟来模拟重新启动它的属性,但实际上我认为你应该在这一点上让自己的类,因为你真正寻找的东西递延没有按T报价。

there is no restart method. You might be able to manually change the properties of a Deferred to simulate "restarting" it, but really I think you should make your own class at this point, as you're really looking for something Deferred doesn't offer.

您必须记住,延迟的设计,使你可以说:嘿递延,去做些(新递延()。当(东西)) ,然后你可以说:嘿延迟,当这样做做X( deferred.then(X))。现在,当你说的时候​​这样做了,即。当你犯了一个。然后通话,递延可以有两种状态:已完成,或没有,但在任何情况下,通话效果(如果它做它立即解决,否则,它等待触发)。

You have to keep in mind, Deferred is designed so that you can say "hey Deferred, go do something" (new Deferred().when(something)), and then you can say "hey Deferred, when that's done do X" (deferred.then(x)). Now, when you say "when that's done", ie. when you make a .then call, the Deferred can be in two states: done, or not, but the call works in either case (if it's done it resolves immediately, otherwise it waits to be triggered).

现在,如果延迟可能有第三国(完成​​,但重新启动),该。然后电话不知道该怎么办:没有做,但重新开始的意思,它应该启动它的thens,还是没有?答案取决于你之前或之后重新启动电话取得了。然后通话,并作为你希望看到的一切都必须得到相当复杂的潜力(特别是如果你允许多个重新启动)。

Now, if a Deferred could have a third state ("done but restarted"), the .then call wouldn't know what to do: does "done but restarted" mean that it should fire it's thens, or not? The answer depends on whether you made the .then call before or after the restart call, and as you're hopefully seeing that all has the potential to get rather complicated (especially if you allow for multiple restarts).

而不是处理所有的(其中没有需要为 $。阿贾克斯)的jQuery的人去的(相对简单)当前延迟实现,我觉得这是对他们的一部分了正确的判罚。

Rather than deal with all that (none of which was needed for $.ajax) the jQuery folks went with the (comparatively simple) current Deferred implementation, and I think it was the right call on their part.