diff --git a/ngStorage.js b/ngStorage.js index b1c2219..f58b23f 100644 --- a/ngStorage.js +++ b/ngStorage.js @@ -154,7 +154,27 @@ // #8, #10: `webStorage.key(i)` may be an empty string (or throw an exception in IE9 if `webStorage` is empty) (k = webStorage.key(i)) && storageKeyPrefix === k.slice(0, prefixLength) && ($storage[k.slice(prefixLength)] = deserializer(webStorage.getItem(k))); } - } + }, + $apply: function() { + var temp$storage; + + _debounce = null; + + if (!angular.equals($storage, _last$storage)) { + temp$storage = angular.copy(_last$storage); + angular.forEach($storage, function(v, k) { + angular.isDefined(v) && '$' !== k[0] && webStorage.setItem(storageKeyPrefix + k, serializer(v)); + + delete temp$storage[k]; + }); + + for (var k in temp$storage) { + webStorage.removeItem(storageKeyPrefix + k); + } + + _last$storage = angular.copy($storage); + } + }, }, _last$storage, _debounce; @@ -164,25 +184,7 @@ _last$storage = angular.copy($storage); $rootScope.$watch(function() { - var temp$storage; - _debounce || (_debounce = $timeout(function() { - _debounce = null; - - if (!angular.equals($storage, _last$storage)) { - temp$storage = angular.copy(_last$storage); - angular.forEach($storage, function(v, k) { - angular.isDefined(v) && '$' !== k[0] && webStorage.setItem(storageKeyPrefix + k, serializer(v)); - - delete temp$storage[k]; - }); - - for (var k in temp$storage) { - webStorage.removeItem(storageKeyPrefix + k); - } - - _last$storage = angular.copy($storage); - } - }, 100, false)); + _debounce || (_debounce = $timeout($storage.$apply, 100, false)); }); // #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent` @@ -196,6 +198,10 @@ } }); + $window.addEventListener && $window.addEventListener('beforeunload', function() { + $storage.$sync(); + }); + return $storage; } ]; diff --git a/test/spec.js b/test/spec.js index 4200478..88cd367 100644 --- a/test/spec.js +++ b/test/spec.js @@ -81,6 +81,7 @@ describe('ngStorage', function() { delete $storage.$default; delete $storage.$reset; delete $storage.$sync; + delete $storage.$apply; expect($storage).to.deep.equal({ string: 'a string', @@ -171,6 +172,7 @@ describe('ngStorage', function() { delete $storage.$default; delete $storage.$reset; delete $storage.$sync; + delete $storage.$apply; expect($storage).to.deep.equal({}); @@ -210,6 +212,7 @@ describe('ngStorage', function() { delete $storage.$default; delete $storage.$reset; delete $storage.$sync; + delete $storage.$apply; expect($storage).to.deep.equal({some: 'value'});