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);
什么时候我会打电话给$。当函数根据数组大小命中服务器时,它会动态调用。
function alwaysFun(res1, res2, res3) {
//works fine prints all the three responses from 3 urls
console.log(res1);
console.log(res2);
console.log(res3);
}
但我的疑问是,如果数组是动态的,包含10个网址[A,B ... Z] 并且响应也将有10个响应,如果它是dyanamic url数组,我怎么能在回调函数中捕获它。 这里我的动态数组网址的方法;
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
}
答案 0 :(得分:1)
您可以使用arguments object,它是一种特殊类型的数组,如对象,它具有传递给方法的所有参数的索引列表。
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)
})
}
演示:Fiddle