Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use jQuery .when() function with a dynamic set of ajax calls?

I'm using JqGrid and for each row in the grid I'm loading, I am making an ajax call to get additional data.

Once that's all complete, I need to apply some formatting.

I would like to use $.when(), but I'm not sure how to call it. I was researching the apply() method, but I still don't see how to use it appropriately.

Here is my code:

 $(rows).each(function () {
                        $.ajax(
                        {
                            url: url,
                            data: data,
                            success: function (result) {

                                }
                            }
                        });
                    });

   $.when(**What do i pass here??**).done(function () {

                    });

I had tried pushing each $.ajax call to an array, but I can't pass the array directly, and call everything.

Thanks in advance for your help!

like image 525
IronicMuffin Avatar asked Jul 11 '11 19:07

IronicMuffin


People also ask

Can jQuery and AJAX be used together?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

What is the use of jQuery AJAX method ()?

jQuery ajax() Method The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

How can show dynamic data in jQuery?

In this example, we should go with the following steps to implement dynamic data loading using jQuery. Create UI to be appended for each user information. Download and import latest version of jQuery library files and jQuery UI files. Create custom jQuery handlers to append UI and load dynamic data into it.

What dataType property of AJAX () method specifies?

The dataType option specifies the type of response data, in this case it is JSON. The timeout parameter specifies request timeout in milliseconds. We have also specified callback functions for error and success. The ajax() method returns an object of jQuery XMLHttpRequest.


1 Answers

This may not work at all, in fact I'm curious as to whether or not it will. Try building the array of promise objects, then call $.when.apply(null, arr).done(function () { ... });

apply allows you to trigger a function and pass an array of arguments dynamically, such as in this case.

like image 104
Dominic Barnes Avatar answered Sep 23 '22 11:09

Dominic Barnes