I'm a scenario, where I should load the multiple methods to get the response for the grid. From the below code, I suppose to load the loadData() function in the read function. Code is showing below.
The loadData function is having nested function. Once the first method is completed I have to call the second method on promise of first method. Once all the method executed. I've to call the parse function.
If I use single restApi It's working fine. But here I have methods method on promise... So how to handle this kind of scenario.
dataSource: new kendo.data.DataSource({
transport: {
read: loadData
},
schema: {
parse: $scope.kendoParse
......
.......
function loadData(){
atFacade.eventDataApi.getCurrentShift().then((data) => {
$scope.fleetListingFilter.currentShift = data;
getAllOperatingSummary();
});
}
function getAllOperatingSummary() {
var startDate = new Date().now() + " " + $scope.fleetListingFilter.currentShift.startTime;
var endDate = new Date().now() + " " + $scope.fleetListingFilter.currentShift.endTime;
var operatingSummary = new at.Models.OperatingSummaryReportFilter(events.PAYLOAD, [], startDate, endDate);
$scope.fleetListingPromise = atFacade.eventDataApi.getOperatingSummaryReportData(operatingSummary).then((data) => {$scope.allVehicles = data;
loadAllVehicles();
});
}
function loadAllVehicles() {
$scope.fleetListingPromise = atFacade.eventDataApi.getAllVehicles().then((data: Array<
any
>) => {
for (var i = 0, total = data.length; i <
total
; i++) {
for (var
j
=
0
, tot = $scope.allVehicles.length; j < tot; j++) {
if (data[i].id === $scope.allVehicles[j].vehicleId) {
data[i].summary = $scope.allVehicles[j];
}
}
}
$
scope.vehicles
=
data
;
$scope.kendoParse(data);
});
}
$scope.kendoParse = (result) => {
var data = [];
var results = atFacade.restangularService.stripRestangular(result);
for (var i = 0; i < results.length; i++) {
var fleetListingData = {
name: results[i].name,
registrationNumber: results[i].registrationNumber,
operator: results[i].summary.operator,
lastStop: results[i].summary.lastStop
};
data.push(fleetListingData);
}
return { "results": data, "totalResults": data.length };
};