1

I´m trying to implement a pagination service. So, in a controller I have two methods to get a list of boats from my database (using a Rails API). To get the data the controller is using a service with a resource (simple query method):

.factory('Boat', 

      function($resource){          
        var boats =
         $resource('http://127.0.0.1\\:3000/:action', {}, {     
            query: {method:'GET', params:{action:'boats'}, isArray:true},
            getBoatsByBusinessId: {method:'GET', params:{action:'boatsByBusinessId'}, isArray:true}
         });        
         return boats;            
      }
  )

In my controller, I have two methods (maybe I could use just one, but I think this is not important this point):

$scope.getOptionList = function(departure, departureDate, arrivalDate, people) { 
  $scope.pagIndex = 0;                      
  $scope.boats = Boat.query({pagIndex: $scope.pagIndex}); // List of boats is filled correctly
};

and OPTION_1:

   $scope.moreResultsBoats = function() {  
        $scope.pagIndex = $scope.pagIndex + 1;                                 
        $scope.moreBoats = Boat.query({pagIndex: $scope.pagIndex});

        console.log("moreBoats:" + $scope.moreBoats);
        angular.forEach($scope.moreBoats, function(boat, key) {             
            $scope.boats.push(boat);
        });
  }

or OPTION_2:

$scope.moreResultsBoats = function() {  
            $scope.pagIndex = $scope.pagIndex + 1;                                 
            $scope.moreBoats = Boat.query({pagIndex: $scope.pagIndex});

            console.log("moreBoats:" + $scope.moreBoats);
            $scope.boats.push.apply($scope.boats, $scope.moreBoats);
}

Neither of the OPTIONS add the array of new boats in $scope.moreBoats to the $scope.boats (or at least the view is not refreshed). I think I don´t understand something about how AngularJS works, because console.log only displays this: moreBoats:.

However, if I add {{moreBoats | json }} to my view, for tracking purposes, I can see the boats in json when they come.

What am I missing?

Thanks, Roberto.

2 Answers 2

4

Thanks for your responses @Maxim. I will try your last solution. Anyway, I have fixed it right now by updating my controller to:

$scope.moreResultsBoats = function() {  
            $scope.pagIndex = $scope.pagIndex + 1;                                 
            Boat.query({pagIndex: $scope.pagIndex}, 
                function success(result) {
                    console.log('Ejecuting callback. Result:' + result);
                    $scope.boats.push.apply($scope.boats, result);      
                }
            );                              
        };

Thanks again!

Sign up to request clarification or add additional context in comments.

Comments

0

Your factory Boat is async and returns the promise.

This is a reason why you get data later in {{moreBoats | json }}.

To make it work you can wrap the promise with .then.

Something like:

Boat.query({pagIndex: $scope.pagIndex})
                    .then(function (result) {
                       $scope.moreBoats = result;                           
                    }, function (result) {
                        alert("Error: No data returned");
                    });  

and the factory:

.factory('Boat',   function($resource){          
        var boats =
         $resource('http://127.0.0.1\\:3000/:action', {}, {     
            query: {method:'GET', params:{action:'boats'}, isArray:true},
            getBoatsByBusinessId: {method:'GET', params:{action:'boatsByBusinessId'}, isArray:true}
         });


         var factory = {
            query: function (selectedSubject) {
                var deferred = $q.defer();
                 deferred.resolve(boats);
                return deferred.promise;
            }
        }
        return factory;         
      }
  )

Reference about promises:

A promise represents a future value, usually a future result of an asynchronous operation, and allows us to define what will happen once this value becomes available, or when an error occurs.

1 Comment

I get this error: TypeError: Object [object Array] has no method 'then' at Object.BoatListCtrl.$scope.moreResultsBoats. I think this is related with AngularJS version. I´m working on AngularJS 1.0.7. Any workaround or other solution for my version?

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.