Reusing jQuery.post () / jQuery.Deferred () object

The simplest example of what I'm looking for is:

var messageLoader = $.post("api/user/messages", {api:data}) messageLoader.done(function(data){ //do something }); 

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

I can't seem to find a call for the Pending object that would allow me to restart it. those. messageLoader.redo() , which ideally would have to re-execute the POST request and subsequently call the same "done" handler, without having to redefine it.

I could put all this into a function and just call that function again, but this is not what I am looking for, because I would like to do it too:

 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 }) 

The idea is that pressing $("#updateMessages") will only repeat this request, then the handler $.when() will use the new messageLoader data and the original friendRequestLoader data.

I looked through the docs for something like this, but found nothing. Perhaps someone here knows if he exists, or a way to accomplish the same thing.

+7
source share
3 answers

I am sure that the Deferred class is simply not designed to work the way you want, because they are designed for one-time use. I even sat in an entire session on Deferreds in jQueryConf, and reusing them was not even mentioned.

In addition, as you can see on the Pending API page:

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

There is no way to restart. You may be able to manually change the Deferred properties to simulate a "reload", but in fact I think you should make your own class at the moment, since you are really looking for something Deferred does not offer.

You must remember that Deferred is designed so that you can say “hey, put it off, do something” ( new Deferred().when(something) ), and then you can say “hey, delayed when it did X” ( deferred.then(x) ). Now when you say "when is this done", i.e. when you make a .then call, Deferred can be in two states: done or not, but the call works anyway (if it resolves right away, otherwise it starts).

Now, if the Deferred can have a third state ("done but restarted"), the .then call would not know what to do: "done but restarted" means that it should start it thens or not? The answer depends on whether you called the .then call before or after the reboot call, and, as you hope, it will be quite complicated (especially if you allow several restarts).

Instead of dealing with all of this (none of which is needed for $.ajax ), the jQuery people went with the (relatively simple) current version of Deferred, and I think that was the right call on their part.

+4
source

It is as simple as creating a function and recalling it. It is not possible to keep $ .post alive because http is a static protocol.

 var messageLoader; var friendRequestLoader; updateMessages(); function updateMessages(){ messageLoader= $.post("api/user/messages", {api:data}) 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(){ updateMessages(); // This doesn't exist }) 
0
source

You can try this method:

 function Defer() { $.when(messageLoader, friendRequestLoader) .done(function (messages, friendRequests) { console.log("M : " + messages[0].test); console.log("F : " + friendRequests[0].test); }); } $("#updateMessages").click(function(){ messageLoader = $.post("api/user/messages", {api:data}); Defer(); }); 

This will allow you to reload it, saving data from another.

0
source

All Articles