I need to fetch data (Q & A) from a distant service using their API. Data is split in different categories and the only methods they offer allow me to list categories and items from a specific category. However, My briefing implies that I gather data from different categories. I ended up doing this (this will gather data from all categories):
var getEveryQA = function(sLang)
{
var allQA = [];
//This request is for getting category listing
$.ajax({
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: category_list_URL,
data: { Lang: sLang },
dataType: "jsonp",
success: function(responseData){
for (var i = 0; i < responseData.length; i++)
{
if(responseData[i].Code.toLowerCase !== "all")//category "all" has no real existence although it is returned in categories listing
{
//Request items for each category
$.ajax({
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: items_by_category_URL,
data: { Lang: sLang, Category: responseData[i].Code },
dataType: "jsonp",
success: function(responseData){
allQA = allQA.concat(responseData);//object from this response will be concatenated to the global object
}
});
}
}
}
});
}
What I would like is to trigger a sorting method whenever all the AJAX calls done in my for
loop have succeeded. I've the feeling jQuery's deferred
is the solution, but the many examples I've read weren't compatible with my for...
loop structure. Is there a way to build some kind of "queue" out of my multiple callbacks that I could then pass as an argument to the deferred
method ? Or maybe am I looking in the wrong direction ?