Search code examples
angularjsajaxdribbble-api

Ionic infinite scroll with dribbble pagination data?


I am messing around with ionic and am using dribbble data just to play around. I am not sure how A. I can query dribble to show posts beyond the first 15, the api is here http://api.dribbble.com/shots/everyone and if I want to get more I can request page 2 like http://api.dribbble.com/shots/everyone?page=2

B. How do I pass the items into my template without conflicting with the old items?

/**
 * Dashboard Controller. (must be authenticated)
 *
 */
.controller('DashController', function($scope, $state, DribbbleData, $http) {

  $scope.items = [];

  $scope.initialize = function() {
    DribbbleData.getData().then(function(res) {
      console.log(res);
      $scope.items = res.data.shots;
    }, function(status) {
      console.log('error')
    })
  }

  $scope.loadMore = function() {
    $http.jsonp('http://api.dribbble.com/shots/everyone?callback=JSON_CALLBACK').success(function(items) {
      console.log(items);
      var test = $scope.items;
      console.log(test)
      //$scope.items = items;
      //useItems(items);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    });
  };

  $scope.$on('$stateChangeSuccess', function() {
    $scope.loadMore();
  });

  $scope.initialize();
})

The View

<ion-view view-title="Dashboard">
  <ion-content class="padding">

    <ion-list>
      <div class="list card" data-ng-repeat="item in items">
        <div class="item item-title">
          <h2>{{item.title}}</h2>
        </div>
        <div class="item item-body">
          <img class="full-image" ng-src="{{item.image_url}}">
          <p>
            <a href="#" class="subdued">{{item.likes_count}} Like</a>
            <a href="#" class="subdued">{{item.comments_count}} Comments</a>
          </p>
        </div>
        <div class="item tabs tabs-secondary tabs-icon-left">
          <a class="tab-item" href="#">
            <i class="icon ion-thumbsup"></i>
            Like
          </a>
          <a class="tab-item" href="#">
            <i class="icon ion-chatbox"></i>
            Comment
          </a>
          <a class="tab-item" href="#">
            <i class="icon ion-share"></i>
            Share
          </a>
        </div>
      </div>
    </ion-list>

    <ion-infinite-scroll on-infinite="loadMore()" distance="1%">
    </ion-infinite-scroll>

  </ion-content>
</ion-view>

Solution

  • If you are sure that there are no duplicates, just concat the items:

    $scope.items = $scope.items.concat(items);
    

    if you are not, the best thing to do is keep track of the items currently in your list, and add only when it does not exist yet:

    var itemIds = {};
    
    function addItems(items) {
      items.forEach(function (item) {
        if (itemsIds[item.id]) {
          return;
        }
        $scope.items.push(item);
        itemIds[item.id] = true;
      }
    }
    

    To handle pagination, just increment a counter in your success callback. For example:

      var nextPage = 2;
    
      $scope.loadMore = function() {
        $http.jsonp('http://api.dribbble.com/shots/everyone?callback=JSON_CALLBACK&page=' + nextPage).success(function(items) {
          nextPage++;
          addItems(items);
          $scope.$broadcast('scroll.infiniteScrollComplete');
        });
      };