auto cookie fallback, fix #5

dev
Julien Bouquillon 11 years ago
parent 9b7415dfa2
commit aade0378f8

@ -11,8 +11,8 @@ angularLocalStorage.constant('prefix', 'ls');
angularLocalStorage.constant('cookie', { expiry:30, path: '/'}); angularLocalStorage.constant('cookie', { expiry:30, path: '/'});
angularLocalStorage.service('localStorageService', [ angularLocalStorage.service('localStorageService', [
'$rootScope', '$rootScope',
'prefix', 'prefix',
'cookie', 'cookie',
function($rootScope, prefix, cookie) { function($rootScope, prefix, cookie) {
@ -25,7 +25,7 @@ angularLocalStorage.service('localStorageService', [
// Checks the browser to see if local storage is supported // Checks the browser to see if local storage is supported
var browserSupportsLocalStorage = function () { var browserSupportsLocalStorage = function () {
try { try {
return ('localStorage' in window && window['localStorage'] !== null); return ('localStorage' in window && window['localStorage'] !== null);
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description); $rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
return false; return false;
@ -40,7 +40,7 @@ angularLocalStorage.service('localStorageService', [
// If this browser does not support local storage use cookies // If this browser does not support local storage use cookies
if (!browserSupportsLocalStorage()) { if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
return false; return addToCookies(key, value);
} }
// 0 and "" is allowed as a value but let's limit other falsey values like "undefined" // 0 and "" is allowed as a value but let's limit other falsey values like "undefined"
@ -50,7 +50,7 @@ angularLocalStorage.service('localStorageService', [
localStorage.setItem(prefix+key, value); localStorage.setItem(prefix+key, value);
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description); $rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
return false; return addToCookies(key, value);
} }
return true; return true;
}; };
@ -60,7 +60,7 @@ angularLocalStorage.service('localStorageService', [
var getFromLocalStorage = function (key) { var getFromLocalStorage = function (key) {
if (!browserSupportsLocalStorage()) { if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
return false; return getFromCookies(key);
} }
var item = localStorage.getItem(prefix+key); var item = localStorage.getItem(prefix+key);
@ -73,14 +73,14 @@ angularLocalStorage.service('localStorageService', [
var removeFromLocalStorage = function (key) { var removeFromLocalStorage = function (key) {
if (!browserSupportsLocalStorage()) { if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
return false; return removeFromCookies(key);
} }
try { try {
localStorage.removeItem(prefix+key); localStorage.removeItem(prefix+key);
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description); $rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
return false; return removeFromCookies(key);
} }
return true; return true;
}; };
@ -92,7 +92,7 @@ angularLocalStorage.service('localStorageService', [
if (!browserSupportsLocalStorage()) { if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
return false; return clearAllFromCookies();
} }
var prefixLength = prefix.length; var prefixLength = prefix.length;
@ -104,7 +104,7 @@ angularLocalStorage.service('localStorageService', [
removeFromLocalStorage(key.substr(prefixLength)); removeFromLocalStorage(key.substr(prefixLength));
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description); $rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
return false; return clearAllFromCookies();
} }
} }
} }
@ -167,7 +167,7 @@ angularLocalStorage.service('localStorageService', [
while (thisCookie.charAt(0)==' ') { while (thisCookie.charAt(0)==' ') {
thisCookie = thisCookie.substring(1,thisCookie.length); thisCookie = thisCookie.substring(1,thisCookie.length);
} }
if (thisCookie.indexOf(prefix+key+'=') == 0) { if (thisCookie.indexOf(prefix+key+'=') === 0) {
return decodeURIComponent(thisCookie.substring(prefix.length+key.length+1,thisCookie.length)); return decodeURIComponent(thisCookie.substring(prefix.length+key.length+1,thisCookie.length));
} }
} }