grevory / angular-local-storage

An AngularJS module that gives you access to the browsers local storage with cookie fallback
Other
2.83k stars 586 forks source link

Caching a multi-page resource #363

Closed bg87 closed 6 years ago

bg87 commented 6 years ago

I'm trying to cache a multi page resource but localStorage.set() is only caching 4 pages out of 6. Has anyone had issues doing this? Caching all of these pages in memory works just fine but saving it to localStorage isn't working. Is there a limit that I don't know about? I'm testing with Chrome which has way more than enough space for what I'm storing.

var promises = [];
        var catalogItems = {
          partInfo: [],
          partNumbers: []
        };
        getCatalogItems(apiConfig.url + 'api/catalogItems?projection=detail')
          .success(function(res) {
            var pages = res.page.totalPages;

            catalogItems = updateCatalogItems(res._embedded.catalogItems, catalogItems);

            if (pages > 1) {
              for (var i = 1; i <= pages; i++) {
                promises.push(
                  getCatalogItems(apiConfig.url + 'api/catalogItems?page=' + i + '&size=1000&projection=detail')
                );
              }

              $q.all(promises).then(function(response) {
                for (var j = 0; j < response.length; j++) {
                  catalogItems = updateCatalogItems(response[j].data._embedded.catalogItems, catalogItems);
                }
                deferred.resolve(catalogItems);
              });
            } else {
              deferred.resolve(catalogItems);
            }
          })
          .error(function(err) {
            deferred.reject(err);
          });
      }
      return deferred.promise;
    }

    function updateCatalogItems(data, catalogItems) {
      data.forEach(function(item) {
        catalogItems.partInfo.push(item);
        catalogItems.partNumbers.push(item.itemNumber);
        item.formattedDate = moment(item.lastModifiedDate).local().format('MM/DD/YYYY');
        item.active = item.active ? 'Y' : 'N';
      });
      localStorageService.set('catalogItems', catalogItems);
      return catalogItems;
    }