1

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;

}
fbid
  • 1,570
  • 2
  • 18
  • 29
  • See https://github.com/firebase/firebase-util/tree/master/src/Paginate#refscrollhasnext – Frank van Puffelen Feb 08 '16 at 15:16
  • Already tried using the`ref.scroll.hasNext()` method. The problem is it always return false so I don't know what kind of property should I use. – fbid Feb 08 '16 at 16:02
  • You should be using `hasNext()` and the snippet you posted does not. If `hasNext()` always returns false, either there's a bug, or you're using it wrong. – Frank van Puffelen Feb 08 '16 at 16:50
  • @FrankvanPuffelen sorry, now I'm editing my original post to include it. – fbid Feb 08 '16 at 17:04

2 Answers2

1

Do the scroll.next in timeout, for example:

 loadMore: function(){
   $timeout(function() {
     scrollRef.scroll.next(4);
   });
 },
Samar Rizvi
  • 300
  • 1
  • 9
0

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.