@@ -62,6 +62,8 @@ angularLocalStorage.provider('localStorageService', function() {
6262 } ;
6363 } ;
6464
65+
66+
6567 this . $get = [ '$rootScope' , '$window' , '$document' , function ( $rootScope , $window , $document ) {
6668
6769 var prefix = this . prefix ;
@@ -79,7 +81,9 @@ angularLocalStorage.provider('localStorageService', function() {
7981 if ( prefix . substr ( - 1 ) !== '.' ) {
8082 prefix = ! ! prefix ? prefix + '.' : '' ;
8183 }
82-
84+ var deriveQualifiedKey = function ( key ) {
85+ return prefix + key ;
86+ }
8387 // Checks the browser to see if local storage is supported
8488 var browserSupportsLocalStorage = ( function ( ) {
8589 try {
@@ -90,7 +94,7 @@ angularLocalStorage.provider('localStorageService', function() {
9094 //
9195 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
9296 // that exceeded the quota."
93- var key = prefix + '__' + Math . round ( Math . random ( ) * 1e7 ) ;
97+ var key = deriveQualifiedKey ( '__' + Math . round ( Math . random ( ) * 1e7 ) ) ;
9498 if ( supported ) {
9599 webStorage . setItem ( key , '' ) ;
96100 webStorage . removeItem ( key ) ;
@@ -127,7 +131,7 @@ angularLocalStorage.provider('localStorageService', function() {
127131 if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
128132 value = angular . toJson ( value ) ;
129133 }
130- webStorage . setItem ( prefix + key , value ) ;
134+ webStorage . setItem ( deriveQualifiedKey ( key ) , value ) ;
131135 if ( notify . setItem ) {
132136 $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : this . storageType } ) ;
133137 }
@@ -147,7 +151,7 @@ angularLocalStorage.provider('localStorageService', function() {
147151 return getFromCookies ( key ) ;
148152 }
149153
150- var item = webStorage . getItem ( prefix + key ) ;
154+ var item = webStorage . getItem ( deriveQualifiedKey ( key ) ) ;
151155 // angular.toJson will convert null to 'null', so a proper conversion is needed
152156 // FIXME not a perfect solution, since a valid 'null' string can't be stored
153157 if ( ! item || item === 'null' ) {
@@ -173,7 +177,7 @@ angularLocalStorage.provider('localStorageService', function() {
173177 }
174178
175179 try {
176- webStorage . removeItem ( prefix + key ) ;
180+ webStorage . removeItem ( deriveQualifiedKey ( key ) ) ;
177181 if ( notify . removeItem ) {
178182 $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : this . storageType } ) ;
179183 }
@@ -286,7 +290,7 @@ angularLocalStorage.provider('localStorageService', function() {
286290 if ( cookie . domain ) {
287291 cookieDomain = "; domain=" + cookie . domain ;
288292 }
289- $document . cookie = prefix + key + "=" + encodeURIComponent ( value ) + expiry + cookiePath + cookieDomain ;
293+ $document . cookie = deriveQualifiedKey ( key ) + "=" + encodeURIComponent ( value ) + expiry + cookiePath + cookieDomain ;
290294 }
291295 } catch ( e ) {
292296 $rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -309,7 +313,7 @@ angularLocalStorage.provider('localStorageService', function() {
309313 while ( thisCookie . charAt ( 0 ) === ' ' ) {
310314 thisCookie = thisCookie . substring ( 1 , thisCookie . length ) ;
311315 }
312- if ( thisCookie . indexOf ( prefix + key + '=' ) === 0 ) {
316+ if ( thisCookie . indexOf ( deriveQualifiedKey ( key ) + '=' ) === 0 ) {
313317 return decodeURIComponent ( thisCookie . substring ( prefix . length + key . length + 1 , thisCookie . length ) ) ;
314318 }
315319 }
@@ -366,6 +370,7 @@ angularLocalStorage.provider('localStorageService', function() {
366370 remove : removeFromLocalStorage ,
367371 clearAll : clearAllFromLocalStorage ,
368372 bind : bindToScope ,
373+ deriveKey : deriveQualifiedKey ,
369374 cookie : {
370375 set : addToCookies ,
371376 add : addToCookies , //DEPRECATED
0 commit comments