@@ -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
@@ -32,6 +35,11 @@ angularLocalStorage.provider('localStorageService', function(){
3235 this . prefix = prefix ;
3336 } ;
3437
38+ // Setter for the storageType
39+ this . setStorageType = function ( storageType ) {
40+ this . storageType = storageType ;
41+ } ;
42+
3543 // Setter for cookie config
3644 this . setStorageCookie = function ( exp , path ) {
3745 this . cookie = {
@@ -59,6 +67,8 @@ angularLocalStorage.provider('localStorageService', function(){
5967 var prefix = this . prefix ;
6068 var cookie = this . cookie ;
6169 var notify = this . notify ;
70+ var storageType = this . storageType ;
71+ var webStorage = $window [ storageType ] ;
6272
6373 // If there is a prefix set in the config lets use that with an appended period for readability
6474 if ( prefix . substr ( - 1 ) !== '.' ) {
@@ -68,7 +78,7 @@ angularLocalStorage.provider('localStorageService', function(){
6878 // Checks the browser to see if local storage is supported
6979 var browserSupportsLocalStorage = ( function ( ) {
7080 try {
71- var supported = ( 'localStorage' in $window && $window [ 'localStorage' ] !== null ) ;
81+ var supported = ( storageType in $window && $window [ storageType ] !== null ) ;
7282
7383 // When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
7484 // is available, but trying to call .setItem throws an exception.
@@ -77,8 +87,8 @@ angularLocalStorage.provider('localStorageService', function(){
7787 // that exceeded the quota."
7888 var key = prefix + '__' + Math . round ( Math . random ( ) * 1e7 ) ;
7989 if ( supported ) {
80- localStorage . setItem ( key , '' ) ;
81- localStorage . removeItem ( key ) ;
90+ webStorage . setItem ( key , '' ) ;
91+ webStorage . removeItem ( key ) ;
8292 }
8393
8494 return true ;
@@ -111,9 +121,9 @@ angularLocalStorage.provider('localStorageService', function(){
111121 if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
112122 value = angular . toJson ( value ) ;
113123 }
114- localStorage . setItem ( prefix + key , value ) ;
124+ webStorage . setItem ( prefix + key , value ) ;
115125 if ( notify . setItem ) {
116- $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : 'localStorage' } ) ;
126+ $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : this . storageType } ) ;
117127 }
118128 } catch ( e ) {
119129 $rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -131,7 +141,7 @@ angularLocalStorage.provider('localStorageService', function(){
131141 return getFromCookies ( key ) ;
132142 }
133143
134- var item = localStorage . getItem ( prefix + key ) ;
144+ var item = webStorage . getItem ( prefix + key ) ;
135145 // angular.toJson will convert null to 'null', so a proper conversion is needed
136146 // FIXME not a perfect solution, since a valid 'null' string can't be stored
137147 if ( ! item || item === 'null' ) {
@@ -157,9 +167,9 @@ angularLocalStorage.provider('localStorageService', function(){
157167 }
158168
159169 try {
160- localStorage . removeItem ( prefix + key ) ;
170+ webStorage . removeItem ( prefix + key ) ;
161171 if ( notify . removeItem ) {
162- $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : 'localStorage' } ) ;
172+ $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : this . storageType } ) ;
163173 }
164174 } catch ( e ) {
165175 $rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -179,7 +189,7 @@ angularLocalStorage.provider('localStorageService', function(){
179189
180190 var prefixLength = prefix . length ;
181191 var keys = [ ] ;
182- for ( var key in localStorage ) {
192+ for ( var key in webStorage ) {
183193 // Only return keys that are for this app
184194 if ( key . substr ( 0 , prefixLength ) === prefix ) {
185195 try {
@@ -211,7 +221,7 @@ angularLocalStorage.provider('localStorageService', function(){
211221
212222 var prefixLength = prefix . length ;
213223
214- for ( var key in localStorage ) {
224+ for ( var key in webStorage ) {
215225 // Only remove items that are for this app and match the regular expression
216226 if ( testRegex . test ( key ) ) {
217227 try {
0 commit comments