Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.when understanding

I am trying to use the jQuery.when to fire two ajax requests and then call some function after the two requests have completed. Here's my code:

var count = 0; var dfr;  var showData = function(data) {     dfr.resolve();     alert(count);    // Do something with my data data received };  var method1 = function() {     dfr = $.Deferred();      return $.ajax('localhost/MyDataService/DataMethod_ReturnsData', {         dataType: "jsonp",         jsonp: "$callback",         success: showData     }); };  var method2 = function() {     return $.ajax('localhost/MyDataService/DataMethod_ReturnsCount', {         dataType: "jsonp",         jsonp: "$callback",         success: function(data) {             count = data.d.__count;         }     }); };  $.when(method1(), method2())     .then(showData()); 

However this is not working as expected. Ajax call in method1 will return data which is to be used in showData() and Ajax call in method2 will return count which is to be assigned to var count and later used in showData().

But when I fire the above code, method1 gets called and then method2 and then showData leaving the data in showData as 'undefined'. How can I achieve this via $.when which as far as I know proceeds only when both functions returning $.promise are executed. I want that both the ajax calls should be called in parallel and later results be displayed based on results from both calls.

like image 524
Ashish Avatar asked Mar 12 '11 05:03

Ashish


People also ask

What is the main purpose of jQuery?

The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

What is .done in jQuery?

done() method in jQuery is used to add handlers which are to be called when the deferred object is resolved. Parameters: Callbacks: This parameter specifies a function, or array of functions, which are called when the Deferred is resolved.

What is $$ jQuery?

jQuery is an object provided by jQuery. $ is another, which is just an alias to jQuery . $$ is not provided by jQuery. It's provided by other libraries, such as Mootools or Prototype. js.

What is deferred jQuery?

Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.


2 Answers

function showData(data1, data2) {     alert(data1[0].max_id);     alert(data2[0].max_id); }  function method1() {     return $.ajax("http://search.twitter.com/search.json", {         data: {             q: 'ashishnjain'         },         dataType: 'jsonp'     }); }  function method2() {     return $.ajax("http://search.twitter.com/search.json", {         data: {             q: 'ashishnjain'         },         dataType: 'jsonp'     }); }  $.when(method1(), method2()).then(showData);​ 

Here's a working jsFiddle

like image 157
Guillaume86 Avatar answered Sep 19 '22 06:09

Guillaume86


The problem is that you're passing showData() to then(), not showData. You should pass a reference to a function to .then():

$.when(method1(), method2())     .then(showData); 

or

$.when(method1(), method2())     .then(function () {         showData();     }); 

Edit

I've put together a working demo. Part of the problem (at least in the code fragment you posted) was that there was no callback function named $callback. Part of the problem was the $ in the callback name '$callback'.

So, remove the jsonp: '$callback' ajax option, so that jQuery defaults to a callback function named callback, and define a function with that name, and it all works.

like image 30
Matt Ball Avatar answered Sep 21 '22 06:09

Matt Ball