In my Angular.JS factory, I am retrieving data from a REST Api.
The REST Api endpoint is "/api/getSsls/1", where 1 represents the page number. The API response is in JSON format and contains the first ten items along with total pages/items information.
I'm trying to create a factory method that fetches all items from the API and iterates through all pages.
This is what I have attempted:
app.factory('Ssls', function ($routeParams,$http) {
allSsls = [];
return {
list:
function (page, callback) {
return $http.get("/api/getSsls/" + page).success(callback);
},
listAll:
function (ssl, callback) {
var TotalPages = ssl.paging.TotalItems/ssl.paging.PageSize;
TotalPages = Math.ceil(TotalPages);
console.log("TOTAL Pages:" + TotalPages);
for(var i = 1; i < TotalPages; i++ ) {
this.list(i,this.processListAll(ssl));
};
},
processListAll:
function (data) {
for( var j = 0; j < data.list.length; j++){
console.log(data.list[j]);
allSsls.push(data.list[j]);
}
}
Next, I invoke this factory method from the controller:
Ssls.list("1",function(data) {
var list = Ssls.listAll(data);
console.log("ALL:" + list);
});
I am facing a couple of issues:
- The array "allSsls" (and "list" in the controller) appears to be empty, indicating a potential problem with variable scope.
- The function "processListAll" only seems to iterate over the dataset of the first page, potentially due to an issue with the callback and parameter passed into it within the code line (this.list(i,this.processListAll(ssl));).
This is my first question on Stack Overflow. Thank you for your assistance!