ionicInfiniteScroll & wordpress


#1

I am using wordpress as a backend for an app and I want to use infinite scroll but I am having trouble concatenating articles.

I am calling the service using a factory:

.factory(‘Worlds’, function ($http) {
var worlds = [];

storageKey = "worlds";

function _getCache() {
    var cache = localStorage.getItem(storageKey );
    if (cache)
        worlds = angular.fromJson(cache);
}
return {
    all: function () {
        return $http.get("http://www.examplesite.com/tna_wp/wp-json/posts?filter[category_name]=international&filter[posts_per_page]=10").then(function (response) {
            worlds = response.data;
            console.log(response.data);
            return worlds;
        });
    },

    GetNewPosts: function () {
        return $http.get("http://www.examplesite.com/tna_wp/wp-json/posts?filter[category_name]=international&filter[posts_per_page]=2").then(function (response) {
            worlds = response.data;
            console.log(response.data);
            return worlds;
        });
    },
    get: function (worldId) {
        if (!worlds.length) 
            _getCache();
        for (var i = 0; i < worlds.length; i++) {
            if (parseInt(worlds[i].ID) === parseInt(worldId)) {
                return worlds[i];
            }
        }
        return null;
    }
}
})

and my controller looks like this:

.controller(‘WorldCtrl’, function ($scope, $stateParams, $timeout, _, Worlds) {
$scope.worlds = [];
Worlds.all().then(function (data){
$scope.worlds = data;
window.localStorage.setItem(“worlds”, JSON.stringify(data));
},

function (err) {
  if(window.localStorage.getItem("worlds") !== undefined) {
    $scope.worlds = JSON.parse(window.localStorage.getItem("worlds"));
  }
}

);

$scope.loadMore = function() {

Worlds.GetNewPosts().then(function (worlds){
    var loadedIdss = _.pluck($scope.worlds, 'id');
    var newItemss = _.reject(worlds, function (item){ 
       return _.contains(loadedIdss, item.id); 
  });
  $scope.worlds = newItemss.concat($scope.worlds);
  $scope.$broadcast('scroll.infiniteScrollComplete');
  });
};

})

I am trying to use underscore to ignore the posts that are already
loaded, however when i try the infinite scroll it just goes into a loop
calling more posts but doesnt add them to my ng-repeat and ionicLoading
renders the app useless.