I have a Angular.JS factory that pulls information from a REST Api.
The REST Api is called via /api/getSsls/1 where 1 is the page number. The API returns a json object with the first ten items as well as information about how many pages/items there are in total.
I want to write a factory method, that gets all items from the api and walks thru all pages.
This is what i tried:
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));
// using this as callback above instead of processListAll(ssl) works and outputs all elements to the console
// function () {
// console.log(data.list[0]);
// });
};
},
processListAll:
function (data) {
for( var j = 0; j < data.list.length; j++){
console.log(data.list[j]);
allSsls.push(data.list[j]);
}
}
I then call this factory method from the controller:
Ssls.list("1",function(data) {
var list = Ssls.listAll(data);
console.log("ALL:" + list);
});
I have a few problems:
- allSsls (as well as list in the controller) seems to be emtpy, probably somethings wrong with the scope of this variables?
- processListAll seems to iterate thru the data set of the first page, probably something with the callback and parameter given to the callback is wrong ( this.list(i,this.processListAll(ssl));)
I'm new to stackoverflow and this is my first question. Thank you for your help!