Pull to refresh data duplication

I am creating an Ionic app that pulls articles from joomla K2 site. I am using $ http and just end my url with 'format = json' and that works fine. However, on the website, I am pulling data from updates to my articles every few minutes, so I need the user to be able to refresh the page. I implemented Ionics pull to update and it works by swelling, except that instead of just pulling in new articles, it just adds all the articles to my array. Is there anything at all, perhaps to iterate over the current timestamps or article IDs (I cache articles in localStorage) to just fetch new articles? My factory looks like this:

.factory('Articles', function ($http) {
    var articles = [];
       storageKey = "articles";

    function _getCache() {
        var cache = localStorage.getItem(storageKey );
        if (cache)
            articles = angular.fromJson(cache);
    }


    return {
        all: function () {
            return $http.get("http://jsonp.afeld.me/?url=http://mexamplesite.com/index.php?format=json").then(function (response) {
                articles = response.data.items;
                console.log(response.data.items);
                return articles;
            });
        },

        getNew: function () {
            return $http.get("http://jsonp.afeld.me/?url=http://mexamplesite.com/index.php?format=json").then(function (response) {
                articles = response.data.items;
                return articles;
            });
        },

        get: function (articleId) {
            if (!articles.length) 
                _getCache();
            for (var i = 0; i < articles.length; i++) {
                if (parseInt(articles[i].id) === parseInt(articleId)) {
                    return articles[i];
                }
            }
            return null;
        }
    }
});

      

and my controller:

.controller('GautengCtrl', function ($scope, $stateParams, $timeout, Articles) {
    $scope.articles = [];
    Articles.all().then(function(data){
        $scope.articles = data;
        window.localStorage.setItem("articles", JSON.stringify(data));
    }, 

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

    );

    $scope.doRefresh = function() {
    Articles.getNew().then(function(articles){
      $scope.articles = articles.concat($scope.articles);
      $scope.$broadcast('scroll.refreshComplete');
    });
  };
})

      

+3


source to share


1 answer


Use underscore.js for simple filtering.

For example:

get all ids of already loaded elements (I believe there are several unique fields like id)

http://underscorejs.org/#pluck

var loadedIds = _.pluck($scope.articles, 'id');

      

Reject all items if item.id is already in the loaded list.

http://underscorejs.org/#reject

http://underscorejs.org/#contains



var newItems = _.reject(articles, function(item){ 
   return _.contains(loadedIds, item.id); 
});

      

Join new items and things:

$scope.articles = newItems.concat($scope.articles);

      

or

http://underscorejs.org/#union

$scope.articles = _.union(newItems, $scope.articles);

      

Actually _.union () can manage and remove duplicates, but I would go for manual filtering with item.id.

+2


source







All Articles