I am trying to create a javascript object for the following scenario
A survey interviews multiple people about food they have consumed over several meals. The object needs to be nested as follows:-
case={}
case[x].Interview={}
case[x].Interview[y].meals={}
case[x].Interview[y].meals[z].Food=[]
I am achieving that through the following codes
var $caseoffset=0
loadcases()
function loadcases() {
$.ajax({
url: "functions.php",data: {offset: $caseoffset,method: "getCase"},method: "post",dataType: 'json',
success: function(result) {
cases = result;
loadinterview(cases[$caseoffset].fldCaseID)
}
})
}
function loadinterview($CaseID) {
$.ajax({
url: "functions.php",
data: {method: "getinterview",caseid: $CaseID}, method: "post",dataType: 'json',
success: function(result) {
thiscase=cases[$caseoffset]
thiscase.interviewcount=result.length
thiscase.interviews={}
$.each(result,function(key,val){
thiscase.interviews[val.fldInterviewID]=val
loadmeals(val.fldInterviewID)
})
}
})
}
function loadmeals($InterviewID) {
$.ajax({
url: "functions.php",
data: {method: "getmeal",InterviewID: $InterviewID},method: "post",dataType: 'json',
success: function(result) {
thiscase.interviews[parseInt($InterviewID)].mealcount = result.length
thiscase.interviews[parseInt($InterviewID)].meals={}
$.each(result, function(key, val) {
thiscase.interviews[parseInt($InterviewID)].meals[parseInt(val.fldMealHistoryID)] = val
getfoodinmeal($InterviewID, val.fldMealHistoryID)
})
}
})
}
function getfoodinmeal($interviewid, $mealid) {
$.ajax({
url: "functions.php",data: {method: "getfoodinmeal",mealid: $mealid},
method: "post",
dataType: 'json',
success: function(result){
foodinmeal = [];
$.each(result, function(key, val) {
foodinmeal.push(val.fldFoodID)
})
thiscase.interviews[$interviewid].meals[$mealid].food = foodinmeal
}
})
}
Problem is that I would like to perform some calculation once all the food consumed by each interviewer has been compiled. How do I create deferred statement to address that.
thiscasevariable like that is not safe if you try to get multiple cases in parallel