如何在完成所有3個異步請求後確保執行self.loadable()。 ???如何確保在完成3個異步功能後執行同步功能
var imghtml = "<span class='pull-right' style='padding-right:25px'><img alt='track' src='app/images/Icons/Track.png'><img alt='expand' src='app/images/Icons/Expand.png'></span>";
var request1 = {};
if(self.serviceid != null)
request1.healthIssue = {id:self.serviceid};
request1.location = {id:self.locationid};
request1.time = {id:header.defaultduration().value};
request1.hospital = {id:header.defaulthospital().value};
request1.query = {groupName:'speciality', dimension:'VISITS', viewBy:'MARKETSHARE'};
console.log(request1);
server.fetchData(request1).done(function(data){
console.log('The specialty marketshares are : ');
console.log(data);
if(data.length != 0) {
$.each(data, function(index,mshare){
var count = 0;
$.each(self.marketsharetable(), function(index, tobj) {
if(tobj.specialty == mshare.name){
tobj.marketshare(mshare.value);
count++;
return false;
}
});
if(count == 0){
var obj = {};
obj.specialty = mshare.name;
obj.marketshare = ko.observable(mshare.value);
obj.rank = ko.observable();
obj.volume = ko.observable();
obj.btns = imghtml;
self.marketsharetable.push(obj);
}
});
console.log(self.marketsharetable());
}
else{
console.log("Clearing the table");
$('#msharetable').DataTable({
"paging": false,
"info": false,
"destroy": true,
"data": self.marketsharetable(),
"language": {
"emptyTable": "No data available"
}
});
}
});
var request2 = {};
if(self.serviceid != null)
request2.healthIssue = {id:self.serviceid};
request2.location = {id:self.locationid};
request2.time = {id:header.defaultduration().value};
request2.hospital = {id:header.defaulthospital().value};
request2.query = {groupName:'speciality', dimension:'VISITS', viewBy:'RANK'};
server.fetchData(request2).done(function(data){
console.log('The specialty ranks are : ');
console.log(data);
$.each(data, function(index,mrank){
var count = 0;
$.each(self.marketsharetable(), function(index, tobj) {
if(tobj.specialty == mrank.name){
tobj.rank(mrank.value);
count++;
return false;
}
});
if(count == 0){
var obj = {};
obj.specialty = mrank.name;
obj.marketshare = ko.observable();
obj.rank = ko.observable(mrank.value);
obj.volume = ko.observable();
obj.btns = imghtml;
self.marketsharetable.push(obj);
}
});
console.log(self.marketsharetable());
});
var request3 = {};
if(self.serviceid != null)
request3.healthIssue = {id:self.serviceid};
request3.location = {id:self.locationid};
request3.time = {id:header.defaultduration().value};
request3.hospital = {id:header.defaulthospital().value};
request3.query = {groupName:'speciality', dimension:'VISITS', viewBy:'COUNT'};
server.fetchData(request3).done(function(data){
console.log('The specialty input volumes are : ');
console.log(data);
$.each(data, function(index,mvolume){
var count = 0;
$.each(self.marketsharetable(), function(index, tobj) {
if(tobj.specialty == mvolume.name){
tobj.volume(mvolume.value);
count++;
return false;
}
});
if(count == 0){
var obj = {};
obj.specialty = mvolume.name;
obj.marketshare = ko.observable();
obj.rank = ko.observable();
obj.volume = ko.observable(mvolume.value);
obj.btns = imghtml;
self.marketsharetable.push(obj);
}
});
console.log(self.marketsharetable());
$('#msharetable').DataTable({
"paging": false,
"info": false,
"destroy": true,
"data": self.marketsharetable(),
"language": {
"emptyTable": "No data available"
},
"deferRender": true,
"columns": [
{ "data": "specialty" },
{ "data": "marketshare" },
{ "data": "rank" },
{ "data": "volume" },
{ "data": "btns" }
]
});
});
self.loadtable();
請幫助如何在3個異步調用服務器之後執行同步功能。對於高級JavaScript來說是新的。建議請問?
我認爲你可以使用承諾來做到這一點,看看這個:https://developer.mozilla。org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – jolmos