0

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!

2
  • listAll() function is not returning anything. So var list should be undefined. How did you check that allSsls is empty? Commented Feb 21, 2015 at 15:00
  • I checked it via chrome console/debugger and by changing the code. What would be the best way to access this data in the controller, by returing it from listAll or by accessing it via var data = Ssls.allSsls; ? Commented Feb 21, 2015 at 20:47

1 Answer 1

1

You're using Ssls.list() with a callback inside your controller, there is no need to return the $http promise from your service, however your main problem is that you're invoking the callback incorrectly. When you execute the callback in your service, you don't pass the data to it! Do this instead:

list:function (page, callback) {
      $http.get("/api/getSsls/" + page).success(function(data){
        callback(data);
      });
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that worked and helped me, the iteration in listAll is now working. What is the best way to return the array sslAll after it has been processed by processListAll to the Controller? And where do I return the http promise in the service and how what would be the better way to implement it?
return $http.get("/api/getSsls/" + page)...the return is unnecessary here as you're using a callback, what you're doing with the callback is fine, there really is no 'better' way, you can return a promise or invoke a callback. Regarding the sslAll array, I'm not sure exactly what it's doing, but you could send it back to the caller (the controller) through a callback just like your other list function, after the processListAll function has finished working on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.