Having an issue with a function that retrieves data based on user checkbox selections. When the user selects "All", the function uses chained promises to fetch the selected data according to the checkbox IDs. The first time it works perfectly, but when the user deselects and reselects "All", the promises do not chain properly. It seems like the original promises are not cleared or something similar.
This function is triggered when the user clicks the "Select All" button:
$scope.selectAll = function () {
$scope.print.sections = angular.copy($scope.sections);
};
When the user clicks "Print Selected", this function is called:
$scope.printSections = function () {
var promise = null;
$scope.print.sections.sort(setOrder);
$scope.dataLoading = true;
var cntr = 0;
var sections = $scope.print.sections;
function next() {
if (cntr < sections.length) {
promise = getSectionData(sections[cntr++]);
promise.then(next);
}
}
next();
};
The function "getSectionData" retrieves data for each section based on its ID. Here is a snippet of the code:
function getSectionData(section) {
var deferred;
deferred = $q.defer();
var id = section.QuestionSectionID;
switch (id) {
case 1:
deferred.resolve(myService.getfirstSection(id)
.success(function (data) {
$scope.firstSection = data;
}));
break;
case 2:
deferred.resolve(myService.getSecondSection(id)
.success(function (data) {
$scope.secondSection= data;
}));
break;
}
return deferred.promise;
}
The issue occurs when trying to chain the promises the second time around in the "next()" function. Any help would be appreciated!