I can't find plugin for jQuery equivalent of this code:
function get() {
var list = ['obj1', 'obj2', 'obj3', 'obj4', 'obj5'],
requests = [];
for(i = 0; i < list.length; i++) {
requests.push($.ajaxQueue({
type: "POST",
url: "/echo/html/",
data: {html: list[i]},
success: function(data) {
$("div").append("Call " + data + "<br>");
}
}));
}
return requests;
}
$(document).on("click", "button", function(){
$.when.apply(null, get()).then(function(){
$.each(arguments, function(key, val){
$("div").append(
"Status: " + val[2].status + ", " +
"responseText: " + val[2].responseText + "<br>"
);
});
});
});
jsfiddle.net
- all after all method
jsfiddle.net
- one by one method
What this code doing:
- Allows to configure ajax requests as one template for all.
- Allows to consistently after each ajax request execute success function.
- Allows to execute function when all requests are done, and provides access to array of jqXHR objects.
How I see this plugin:
$.ajaxMulti({
type: "POST",
url: arrayOfUrls,
data: {name: ""}
// other ajax options
})
.oneDone(function(data){
// manipulate with data (sequence observed)
})
.allDone(function(objects){
// execute when all requests done
});
Helpful links: