10

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!

IronicMuffin
  • 4,182
  • 12
  • 47
  • 90

1 Answers1

13

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.

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
  • That's what I was trying, but apply doesn't return a jquery object, right? So the .done() won't be defined. I'm in the process of testing that now. – IronicMuffin Jul 11 '11 at 19:41
  • 1
    Calling `apply` should return whatever the function being triggered would return. Again, I'm not sure if the context (where I passed in `null`) is important. You could try `$` in place of `null`. – Dominic Barnes Jul 11 '11 at 19:42
  • Also, how do I get the promise objects? If I wrap each ajax call in a push to an array, I get all the ajax objects, but they don't execute... – IronicMuffin Jul 11 '11 at 19:44
  • 1
    `var promise = $.ajax({ ... }); arr.push(promise);` – Dominic Barnes Jul 11 '11 at 19:45
  • Wow, that's a nice way to use apply. I though apply was only to define the context of a function. Thanks! –  Dec 04 '13 at 21:59