jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

Illegal operation when using local-/sessionStorage #148

Closed thany closed 9 years ago

thany commented 9 years ago

When I use a cache that has storageMode set to either "localStorage" or "sessionStorage", I get this error whenever I (a $resource) is trying to use it:

Illegal operation on WrappedNative prototype object

In angular-1.3.2.js on line 279.

Full stack:

isArrayLike@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:279:6
forEach@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:335:31
copy@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:833:8
copy@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:799:22
copy@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:839:19
copy@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:799:22
resourceFactory/</Resource[name]/<@http://192.168.1.45:8087/includes/js/vendor/angular-resource-1.3.2.js:569:34
forEach@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:347:10
resourceFactory/</Resource[name]@http://192.168.1.45:8087/includes/js/vendor/angular-resource-1.3.2.js:567:12
@http://192.168.1.45:8087/includes/js/productfinder/controllers.js:12:14 invoke@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:4118:13
instantiate@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:4129:22
$ControllerProvider/this.$get</<@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:8368:17
ngViewFillContentFactory/<.link@http://192.168.1.45:8087/includes/js/vendor/angular-route-1.3.2.js:968:25
invokeLinkFn@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:8129:8
nodeLinkFn@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:7641:0
compositeLinkFn@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:6997:12
publicLinkFn@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:6876:29
createBoundTranscludeFn/boundTranscludeFn@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:7015:0
controllersBoundTransclude@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:7668:17
update@http://192.168.1.45:8087/includes/js/vendor/angular-route-1.3.2.js:926:24
$RootScopeProvider/this.$get</Scope.prototype.$broadcast@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:14522:14
commitRoute/<@http://192.168.1.45:8087/includes/js/vendor/angular-route-1.3.2.js:608:14
processQueue@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:12988:26
scheduleProcessQueue/<@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:13004:26
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:14204:15
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:14020:14
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:14308:12
done@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:9522:35
completeRequest@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:9707:6
requestLoaded@http://192.168.1.45:8087/includes/js/vendor/angular-1.3.2.js:9650:0

The version numbers are right there. Browser in question is Firefox 33.

thany commented 9 years ago

Temporary workaround is to use storageImpl, like so:

app.factory("JsonCache", ["DSCacheFactory", function jsonCacheFactory(DSCacheFactory) {
  var cacheImpl = {
    setItem: function(key, value) {
      window.sessionStorage.setItem(key, value);
    },
    getItem: function(key) {
      return window.sessionStorage.getItem(key);
    },
    removeItem: function(key) {
      window.sessionStorage.removeItem(key);
    }
  };
  return DSCacheFactory("json-cache", {
    // more config
    storageMode: window.sessionStorage ? "sessionStorage" : "memory",
    storageImpl: window.sessionStorage ? cacheImpl : undefined
  });
}]);

This seems to work fine. Although I'd expect storageMode: "sessionStorage" to work as well, so this is not quite a fix. But good to know that it can work.

jmdobry commented 9 years ago

I think I found the issue. See #122

thany commented 9 years ago

Ah, makes sense. That is why my workaround works :)