Skip to content

Commit

Permalink
Sync is now done without clearing the whole Storage (#1); Changes hap…
Browse files Browse the repository at this point in the history
…pening in other browser tabs will now be sync'd as well
  • Loading branch information
Gias Kay Lee committed Jul 18, 2013
1 parent b80f1da commit b463d06
Showing 1 changed file with 69 additions and 56 deletions.
125 changes: 69 additions & 56 deletions ngStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,82 @@

(function() {

/**
* @ngdoc overview
* @name ngStorage
*/

angular.module('ngStorage', []).

/**
* @ngdoc object
* @name ngStorage.$localStorage
* @requires $browser
* @requires $window
*/

factory('$localStorage', _storageFactory('localStorage')).

/**
* @ngdoc object
* @name ngStorage.$sessionStorage
* @requires $browser
* @requires $window
*/

factory('$sessionStorage', _storageFactory('sessionStorage'));

function _storageFactory(storageType) {
return function(
$browser,
$window
){
var webStorage = $window[storageType],
storage = {
$clear: function() {
for (var k in storage) {
'$clear' === k || delete storage[k];
/**
* @ngdoc overview
* @name ngStorage
*/

angular.module('ngStorage', []).

/**
* @ngdoc object
* @name ngStorage.$localStorage
* @requires $browser
* @requires $window
*/

factory('$localStorage', _storageFactory('localStorage')).

/**
* @ngdoc object
* @name ngStorage.$sessionStorage
* @requires $browser
* @requires $window
*/

factory('$sessionStorage', _storageFactory('sessionStorage'));

function _storageFactory(storageType) {
return function(
$rootScope,
$browser,
$window
){
var webStorage = $window[storageType],
$storage = {
$clear: function() {
for (var k in $storage) {
'$clear' === k || delete $storage[k];
}
}
}
},
lastStorage;
},
_last$storage;

for (var i = 0, k; k = webStorage.key(i); i++) {
storage[k] = angular.fromJson(webStorage.getItem(k));
}
for (var i = 0, k; k = webStorage.key(i); i++) {
$storage[k] = angular.fromJson(webStorage.getItem(k));
}

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

$browser.addPollFn(function() {
if (!angular.equals(storage, lastStorage)) {
$browser.addPollFn(function() {
if (!angular.equals($storage, _last$storage)) {

webStorage.clear();
angular.forEach($storage, function(v, k) {
if (angular.isDefined(v) && '$clear' !== k) {
webStorage.setItem(k, angular.toJson(v));
}

angular.forEach(storage, function(v, k) {
if (angular.isDefined(v) && '$clear' !== k) {
webStorage.setItem(k, angular.toJson(v));
}
});
delete _last$storage[k];
});

lastStorage = angular.copy(storage);
}
});
angular.forEach(_last$storage, function(v, k) {
webStorage.removeItem(k);
});

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

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

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

$rootScope.$digest();
});

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

})();

0 comments on commit b463d06

Please sign in to comment.