*Updated - DIDN'T WORK AS I EXPECTED (see real solution below)
Failed Approach
I found a way out using eval
... it is not fully tested but seems ok. Please feels free to post your comments/trade-off.
callAjax
is a method that receive an url and return a promise such as return $.ajax(...)
, but since I should executed it using .when
statement I put all the calls in a string to later eval them inside the .when
statement.
convertAjaxCallsToString = function () {
var result = '';
for (var i = 0; i < ajaxservices.length; i++) {
result += "callAjax(ajaxservices[" + i + "])";
if (i + 1 < ajaxservices.length) {
result += ",";
}
}
return result;
}
Since I don't expect more than 10 ajax calls, I hardcoded these 10 responses (arg 0 - 9) as you see below... If I only use 3 ajax services, then only 3 arg should have value and the others will be undefined
and never will be eval because callbacks.length
should be 3 as well.
var ajaxcalls = _self.convertAjaxCallsToString();
$.when(eval(ajaxcalls)).done(function (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
for (var i = 0; i < callbacks.length; i++) {
//If you expect a page response, Each argument is an array with the following structure: [ data, statusText, jqXHR ]
var data = eval('arg' + i)[0];
callbacks[i](data);
}
});
Posible Solution
the call ajax didn't work as I expected so I decided to use another approach. Tested and works fine!
$.when(ajax(0), ajax(1), ajax(2), ajax(3), ajax(4), ajax(5), ajax(6), ajax(7), ajax(8), ajax(9))
.done(function (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
for (var i = 0; i < callbacks.length; i++) {
//If you expect a page response, Each argument is an array with the following structure: [ data, statusText, jqXHR ]
var data = eval('arg' + i)[0];
callbacks[i](data);
}
});
Where ajax is will return 0 if there is not ajax service to call.
var ajax = function (index) {
if (index < ajaxservices.length) {
return callAjax(ajaxservices[index]);
}
return 0;
}