Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/0.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gias Kay Lee committed Aug 9, 2013
2 parents a4a5fdc + ca10bb8 commit 8fb19d6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 56 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ angular.module('app', [
){});
```

### Read and Write | [Demo](http://plnkr.co/edit/3vfRkvG7R9DgQxtWbGHz)
### Read and Write | [Demo](http://plnkr.co/edit/3vfRkvG7R9DgQxtWbGHz?p=preview)

Pass `$localStorage` (or `$sessionStorage`) by reference to a hook under `$scope` in plain ol' JavaScript:

Expand All @@ -61,7 +61,7 @@ And use it like you-already-know:
With this setup, changes will be automatically sync'd between `$scope.$storage`, `$localStorage`, and localStorage - even across different browser tabs!
### Read and Write Alternative (Not Recommended) | [Demo](http://plnkr.co/edit/9ZmkzRkYzS3iZkG8J5IK)
### Read and Write Alternative (Not Recommended) | [Demo](http://plnkr.co/edit/9ZmkzRkYzS3iZkG8J5IK?p=preview)
If you're not fond of the presence of `$scope.$storage`, you can always use watchers:
Expand All @@ -81,7 +81,7 @@ $scope.$watch(function() {
This, however, is not the way ngStorage is designed to be used with. As can be easily seen by comparing the demos, this approach is way more verbose, and may have potential performance implications as the values being watched quickly grow.

### Delete | [Demo](http://plnkr.co/edit/o4w3VGqmp8opfrWzvsJy)
### Delete | [Demo](http://plnkr.co/edit/o4w3VGqmp8opfrWzvsJy?p=preview)

Plain ol' JavaScript again, what else could you better expect?

Expand All @@ -93,7 +93,7 @@ delete $localStorage.counter;

This will delete the corresponding entry inside the Web Storage.

### Delete Everything | [Demo](http://plnkr.co/edit/YiG28KTFdkeFXskolZqs)
### Delete Everything | [Demo](http://plnkr.co/edit/YiG28KTFdkeFXskolZqs?p=preview)

If you wish to clear the Storage in one go, use the `$reset()` method:

Expand All @@ -109,7 +109,7 @@ $localStorage.$reset();
> });
> ```
### Permitted Values | [Demo](http://plnkr.co/edit/n0acYLdhk3AeZmPOGY9Z)
### Permitted Values | [Demo](http://plnkr.co/edit/n0acYLdhk3AeZmPOGY9Z?p=preview)
You can store anything except those [not supported by JSON](http://www.json.org/js.html):
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngstorage",
"version": "0.2.1",
"version": "0.2.2",
"main": "./ngStorage.js",
"dependencies": {
"angular": "1.0.7"
Expand Down
103 changes: 53 additions & 50 deletions ngStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,72 +35,75 @@
'$browser',
'$window',

function(
$rootScope,
$browser,
$window
){
var webStorage = $window[storageType],
$storage = {
$default: function(items) {
for (var k in items) {
angular.isDefined($storage[k]) || ($storage[k] = items[k]);
function(
$rootScope,
$browser,
$window
){
var webStorage = $window[storageType],
$storage = {
$default: function(items) {
for (var k in items) {
angular.isDefined($storage[k]) || ($storage[k] = items[k]);
}

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

return $storage.$default(items);
}

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

return $storage.$default(items);
}
},
_last$storage;
// (#8) `i < webStorage.length` is needed for IE9
for (var i = 0, k; i < webStorage.length && (k = webStorage.key(i)); i++) {
'ngStorage-' === k.slice(0, 10) && ($storage[k.slice(10)] = angular.fromJson(webStorage.getItem(k)));
}

for (var i = 0, k; i < webStorage.length && (k = webStorage.key(i)); i++) {
'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) && '$' !== k[0]) {

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

$browser.addPollFn(function() {
if (!angular.equals($storage, _last$storage)) {
angular.forEach($storage, function(v, k) {
if (angular.isDefined(v) && '$' !== k[0]) {
webStorage.setItem('ngStorage-' + k, angular.toJson(v));
}

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

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

delete _last$storage[k];
});
_last$storage = angular.copy($storage);

for (var k in _last$storage) {
webStorage.removeItem('ngStorage-' + k);
$rootScope.$apply();
}
});

_last$storage = angular.copy($storage);
// (#6) Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent`
'localStorage' === storageType && $window.addEventListener('storage', function(event) {
if ('ngStorage-' === event.key.slice(0, 10)) {
event.newValue ? $storage[event.key.slice(10)] = angular.fromJson(event.newValue) : delete $storage[event.key.slice(10)];

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

'localStorage' === storageType && angular.element($window).bind('storage', function(event) {
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.$apply();
}
});
$rootScope.$apply();
}
});

return $storage;
}];
return $storage;
}
];
}

})();

0 comments on commit 8fb19d6

Please sign in to comment.