Skip to content

Commit 4f71583

Browse files
author
paulo.neves
committed
add sessionstorage to localStorage provider.
1 parent 233ef1a commit 4f71583

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

angular-local-storage.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ angularLocalStorage.provider('localStorageService', function(){
1313

1414
this.prefix = 'ls';
1515

16+
// You could change web storage type localstorage or sessionStorage
17+
this.storageType='localStorage';
18+
1619
// Cookie options (usually in case of fallback)
1720
// expiry = Number of days before cookies expire // 0 = Does not expire
1821
// path = The web path the cookie represents
@@ -54,6 +57,8 @@ angularLocalStorage.provider('localStorageService', function(){
5457
var prefix = this.prefix;
5558
var cookie = this.cookie;
5659
var notify = this.notify;
60+
var storageType = this.storageType;
61+
var webStorage = $window[storageType];
5762

5863
// If there is a prefix set in the config lets use that with an appended period for readability
5964
if (prefix.substr(-1) !== '.') {
@@ -63,7 +68,7 @@ angularLocalStorage.provider('localStorageService', function(){
6368
// Checks the browser to see if local storage is supported
6469
var browserSupportsLocalStorage = (function () {
6570
try {
66-
var supported = ('localStorage' in $window && $window['localStorage'] !== null);
71+
var supported = (storageType in $window && $window[storageType] !== null);
6772

6873
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
6974
// is available, but trying to call .setItem throws an exception.
@@ -72,8 +77,8 @@ angularLocalStorage.provider('localStorageService', function(){
7277
// that exceeded the quota."
7378
var key = prefix + '__' + Math.round(Math.random() * 1e7);
7479
if (supported) {
75-
localStorage.setItem(key, '');
76-
localStorage.removeItem(key);
80+
webStorage.setItem(key, '');
81+
webStorage.removeItem(key);
7782
}
7883

7984
return true;
@@ -106,9 +111,9 @@ angularLocalStorage.provider('localStorageService', function(){
106111
if (angular.isObject(value) || angular.isArray(value)) {
107112
value = angular.toJson(value);
108113
}
109-
localStorage.setItem(prefix + key, value);
114+
webStorage.setItem(prefix + key, value);
110115
if (notify.setItem) {
111-
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'localStorage'});
116+
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: this.storageType});
112117
}
113118
} catch (e) {
114119
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
@@ -126,7 +131,7 @@ angularLocalStorage.provider('localStorageService', function(){
126131
return getFromCookies(key);
127132
}
128133

129-
var item = localStorage.getItem(prefix + key);
134+
var item = webStorage.getItem(prefix + key);
130135
// angular.toJson will convert null to 'null', so a proper conversion is needed
131136
// FIXME not a perfect solution, since a valid 'null' string can't be stored
132137
if (!item || item === 'null') {
@@ -152,9 +157,9 @@ angularLocalStorage.provider('localStorageService', function(){
152157
}
153158

154159
try {
155-
localStorage.removeItem(prefix+key);
160+
webStorage.removeItem(prefix+key);
156161
if (notify.removeItem) {
157-
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'localStorage'});
162+
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: this.storageType});
158163
}
159164
} catch (e) {
160165
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
@@ -174,7 +179,7 @@ angularLocalStorage.provider('localStorageService', function(){
174179

175180
var prefixLength = prefix.length;
176181
var keys = [];
177-
for (var key in localStorage) {
182+
for (var key in webStorage) {
178183
// Only return keys that are for this app
179184
if (key.substr(0,prefixLength) === prefix) {
180185
try {
@@ -206,7 +211,7 @@ angularLocalStorage.provider('localStorageService', function(){
206211

207212
var prefixLength = prefix.length;
208213

209-
for (var key in localStorage) {
214+
for (var key in webStorage) {
210215
// Only remove items that are for this app and match the regular expression
211216
if (testRegex.test(key)) {
212217
try {

0 commit comments

Comments
 (0)