Skip to content

Commit

Permalink
Add namespace to Web Storage keys (Fix #3); A few additional code ref…
Browse files Browse the repository at this point in the history
…inements
  • Loading branch information
Gias Kay Lee committed Jul 24, 2013
1 parent 9e1adf7 commit 8e34a23
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions ngStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,62 +38,60 @@
var webStorage = $window[storageType],
$storage = {
$default: function(items) {
angular.forEach(items, function(v, k) {
angular.isDefined($storage[k]) || ($storage[k] = v);
});
for (var k in items) {
angular.isDefined($storage[k]) || ($storage[k] = items[k]);
}

return $storage;
},
$reset: function(items) {
for (var k in $storage) {
/^\$/.test(k) || delete $storage[k];
'$' === k[0] || delete $storage[k];
}

angular.forEach(items, function(v, k) {
$storage[k] = v;
});

return $storage;
return $storage.$default(items);
}
},
_last$storage;

for (var i = 0, k; k = webStorage.key(i); i++) {
$storage[k] = angular.fromJson(webStorage.getItem(k));
'ngStorage-' === k.slice(0, 10) && ($storage[k.slice(10)] = angular.fromJson(webStorage.getItem(k)));
}

_last$storage = angular.copy($storage);

$browser.addPollFn(function() {
if (!angular.equals($storage, _last$storage)) {
angular.forEach($storage, function(v, k) {
if (angular.isDefined(v) && !/^\$/.test(k)) {
if (angular.isDefined(v) && '$' !== k[0]) {

// Remove $$hashKey and other things that cannot be stringified
$storage[k] = angular.fromJson(angular.toJson(v));

webStorage.setItem(k, angular.toJson(v));
webStorage.setItem('ngStorage-' + k, angular.toJson(v));
}

delete _last$storage[k];
});

angular.forEach(_last$storage, function(v, k) {
webStorage.removeItem(k);
});
for (var k in _last$storage) {
webStorage.removeItem('ngStorage-' + k);
}

_last$storage = angular.copy($storage);

$rootScope.$digest();
$rootScope.$apply();
}
});

'localStorage' === storageType && angular.element($window).bind('storage', function(event) {
event.newValue ? $storage[event.key] = angular.fromJson(event.newValue) : delete $storage[event.key];
if ('ngStorage-' === event.key.slice(0, 10)) {
event.newValue ? $storage[event.key.slice(10)] = angular.fromJson(event.newValue) : delete $storage[event.key.slice(10)];

_last$storage = angular.copy($storage);
_last$storage = angular.copy($storage);

$rootScope.$digest();
$rootScope.$apply();
}
});

return $storage;
Expand Down

0 comments on commit 8e34a23

Please sign in to comment.