Reputation: 505
var urlArray = ["api/vehicle?sttime=1424044800&endtime=1424390400","api/vehicle?type=travel&sttime=1424476800&endtime=1424822400","api/vehicle/?type=travel&sttime=1424908800&endtime=1425859199"]
function succ(eachurl) {
return _.http.get(eachurl);
//calling ajax using _.http.get(url)
}
var ps = _.map(urlArray, succ);
//and ps will be [{"readyState":1},{"readyState":1},{"readyState":1}]
$.when.apply($, ps).always(alwaysFun);
when ever i will call $.when function will hit server based on array size which is dynamic and calls.
function alwaysFun(res1, res2, res3) {
//works fine prints all the three responses from 3 urls
console.log(res1);
console.log(res2);
console.log(res3);
}
But my Doubt is if the array is dynamic containing 10 urls [A, B ...Z] and the response will also 10 responses and how can i will capture it in call back function if it is dyanamic array of url are hitted. here my approach for the dynamic array urls;
ps = [A, b...z];
$.when.apply($, ps).always(alwaysFun);
//How can i will mention dynamic responses in call back please
function alwaysFun(res1, res2 ....resZ) {
//Please guide on this issue
}
Upvotes: 0
Views: 52
Reputation: 388436
You can use the arguments object, it is a special type of array like object which has an indexed list of all arguments passed to the method.
function alwaysFun(res1, res2, res3) {
//works fine prints all the three responses from 3 urls
console.log(res1);
console.log(res2);
console.log(res3);
//user
console.log('using arguments')
$.each(arguments, function (i, arg) {
console.log(arg)
})
}
Demo: Fiddle
Upvotes: 1