2

My service populates all items using $http and a cache when the controller is activated. If I create a new item, by doing an $http.post(), what is the best way to refresh the cache?

The problem with the example below is the cached getAll call will return an outdated array:

(function () {

    'use strict';

    angular
        .module('myapp')
        .factory('items', itemsService)
        .controller('Items', itemsController);

    // myCache was created using angular-cache

    itemsService.$inject = ['$http', 'myCache'];
    function itemsService ($http, myCache) {

        var service = {
            getAll : function () {
                return $http.get('/api/item', myCache);
            },
            createNew : function (item) {
                return $http.post('/api/item', item);
            }
        };

        return service;
    }

    itemsController.$inject = ['items'];
    function itemsController (items) {

        var vm = this;
        vm.items = [];
        vm.item = {};

        activate();

        function activate() {
            items.getAll().then(function(response){
                vm.items = response.data || [];
            });
        }

        function createNew() {
            items.createNew(vm.item).then(function(response){
                vm.items.push(response.data);
            });
        }
    }

})();

Edit #1 : Invalidating Cache

I've modified the code to invalidate the cache when a new item is created. The addition of the $q service, and manually rejecting or resolveing the calls seems very tedious and bloated.

Is there a better way?

(function () {

    'use strict';

    angular
        .module('myapp')
        .factory('items', itemsService)
        .controller('Items', itemsController);

    itemsService.$inject = ['$q', '$http', 'CacheFactory'];
    function itemsService ($q, $http, CacheFactory) {

        var _cache = CacheFactory.get('items') || CacheFactory('items');

        var service = {
            getItems : function(refresh) {
                var d = $q.defer();
                if (refresh) { _cache.invalidate(); }
                $http.get('/api/item', _cache).then(function(response){
                    d.resolve(response.data);
                }, function(err){ d.reject(err); });
                return d.promise;
            },
            createNew   : function(info){
                var d = $q.defer();
                $http.post('/api/item', info).then(function(response){
                    _cache.invalidate();
                    d.resolve(response.data);
                }, function(err){ d.reject(err); });
                return d.promise;
            }
        };

        return service;
    }

    itemsController.$inject = ['items'];
    function itemsController (items) {

        var vm = this;
        vm.items = [];
        vm.item = {};

        activate();

        function activate() {
            items.getAll().then(function(response){
                vm.items = response.data || [];
            });
        }

        function createNew() {
            items.createNew(vm.item).then(function(response){
                vm.items.push(response.data);
            });
        }
    }

})();
G. Deward
  • 1,542
  • 3
  • 17
  • 30

0 Answers0