3
3
'use strict' ;
4
4
var angularLocalStorage = angular . module ( 'LocalStorageModule' , [ ] ) ;
5
5
6
- angularLocalStorage . provider ( 'localStorageService' , function ( ) {
6
+ angularLocalStorage . provider ( 'localStorageService' , function ( ) {
7
+
7
8
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app
8
9
// e.g. localStorageServiceProvider.setPrefix('youAppName');
9
10
// With provider you can use config as this:
10
11
// myApp.config(function (localStorageServiceProvider) {
11
12
// localStorageServiceProvider.prefix = 'yourAppName';
12
13
// });
13
-
14
14
this . prefix = 'ls' ;
15
15
16
16
// You could change web storage type localstorage or sessionStorage
17
- this . storageType = 'localStorage' ;
17
+ this . storageType = 'localStorage' ;
18
18
19
19
// Cookie options (usually in case of fallback)
20
20
// expiry = Number of days before cookies expire // 0 = Does not expire
@@ -31,38 +31,38 @@ angularLocalStorage.provider('localStorageService', function(){
31
31
} ;
32
32
33
33
// Setter for the prefix
34
- this . setPrefix = function ( prefix ) {
34
+ this . setPrefix = function ( prefix ) {
35
35
this . prefix = prefix ;
36
36
} ;
37
37
38
38
// Setter for the storageType
39
- this . setStorageType = function ( storageType ) {
39
+ this . setStorageType = function ( storageType ) {
40
40
this . storageType = storageType ;
41
41
} ;
42
42
43
43
// Setter for cookie config
44
- this . setStorageCookie = function ( exp , path ) {
44
+ this . setStorageCookie = function ( exp , path ) {
45
45
this . cookie = {
46
46
expiry : exp ,
47
47
path : path
48
48
} ;
49
49
} ;
50
50
51
51
// Setter for cookie domain
52
- this . setStorageCookieDomain = function ( domain ) {
52
+ this . setStorageCookieDomain = function ( domain ) {
53
53
this . cookie . domain = domain ;
54
54
} ;
55
55
56
56
// Setter for notification config
57
57
// itemSet & itemRemove should be booleans
58
- this . setNotify = function ( itemSet , itemRemove ) {
58
+ this . setNotify = function ( itemSet , itemRemove ) {
59
59
this . notify = {
60
60
setItem : itemSet ,
61
61
removeItem : itemRemove
62
62
} ;
63
63
} ;
64
64
65
- this . $get = [ '$rootScope' , '$window' , '$document' , function ( $rootScope , $window , $document ) {
65
+ this . $get = [ '$rootScope' , '$window' , '$document' , function ( $rootScope , $window , $document ) {
66
66
67
67
var prefix = this . prefix ;
68
68
var cookie = this . cookie ;
@@ -93,6 +93,7 @@ angularLocalStorage.provider('localStorageService', function(){
93
93
94
94
return true ;
95
95
} catch ( e ) {
96
+ storageType = 'cookie' ;
96
97
$rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
97
98
return false ;
98
99
}
@@ -298,9 +299,9 @@ angularLocalStorage.provider('localStorageService', function(){
298
299
}
299
300
300
301
var cookies = $document . cookie && $document . cookie . split ( ';' ) || [ ] ;
301
- for ( var i = 0 ; i < cookies . length ; i ++ ) {
302
+ for ( var i = 0 ; i < cookies . length ; i ++ ) {
302
303
var thisCookie = cookies [ i ] ;
303
- while ( thisCookie . charAt ( 0 ) === ' ' ) {
304
+ while ( thisCookie . charAt ( 0 ) === ' ' ) {
304
305
thisCookie = thisCookie . substring ( 1 , thisCookie . length ) ;
305
306
}
306
307
if ( thisCookie . indexOf ( prefix + key + '=' ) === 0 ) {
@@ -330,8 +331,13 @@ angularLocalStorage.provider('localStorageService', function(){
330
331
}
331
332
} ;
332
333
334
+ var getStorageType = function ( ) {
335
+ return storageType ;
336
+ } ;
337
+
333
338
return {
334
339
isSupported : browserSupportsLocalStorage ,
340
+ getStorageType : getStorageType ,
335
341
set : addToLocalStorage ,
336
342
add : addToLocalStorage , //DEPRECATED
337
343
get : getFromLocalStorage ,
0 commit comments