How to check if all documents are loaded with Firebase.util pagination

How can I check if I have to stop calling the loadMore() function, because all the documents have already been loaded from the database?

In the example below I'm using Ionic, but it's the same also with ng-infinite-scroll in AngularJS apps.

This is my actual code:

HTML:

  ...

  <ion-infinite-scroll
    ng-if="!noMoreItemsToLoad"
    on-infinite="loadMore()"
    distance="5%">
  </ion-infinite-scroll>

</ion-content>

JS Controller:

$scope.loadMore = function(){

  console.log('Loading more docs...');

  Items.loadMore();  // calling the .next() method inside the Items service

  if( !Items.hasNext()) { $scope.noMoreItemsToLoad = true; }

  $scope.$broadcast('scroll.infiniteScrollComplete');

}

JS Items Factory:

.factory('Items', function (FIREBASE_URL, $firebaseArray,  $firebaseObject) {

  var itemsRef = new Firebase(FIREBASE_URL + 'items/');
  var scrollRef = new Firebase.util.Scroll(itemsRef, 'name');

  var self = {
     getAllItems : function(){ ... },

     loadMore: function(){
       scrollRef.scroll.next(4);
     },

     hasNext: function(){
       if(scrollRef.scroll.hasNext()) { return true; }
       else { return false; }
     }
  }

  return self;

}

Answers


Do the scroll.next in timeout, for example:

 loadMore: function(){
   $timeout(function() {
     scrollRef.scroll.next(4);
   });
 },

I had the same issue and I think the solution is to modify the hasNext() function on firebase.util.js:

Cache.prototype.hasNext = function() {
  return this.count === -1 || this.endCount >= this.start + this.count;
};

I put a missing equal sign (=) before this.start

I hope it works for you.


Need Your Help

Why aren't programs written in Assembly more often?

c programming-languages assembly

It seems to be a mainstream opinion that assembly programming takes longer and is more difficult to program in than a higher level language such as C. Therefore it seems to be recommend or assumed ...

Access elements of returned array in Fortran

arrays fortran element

How does one access an element of an array that is returned from a function? For example, shape() returns an array of integers. How does one compare an element of that array to an integer? The foll...