Update localStorageModule.js

Configurable notifications of add and remove items.
dev
lucuma 11 years ago
parent fa727e840c
commit 255fafe769

@ -9,12 +9,14 @@ angularLocalStorage.constant('prefix', 'ls');
// expiry = Number of days before cookies expire // 0 = Does not expire // expiry = Number of days before cookies expire // 0 = Does not expire
// path = The web path the cookie represents // path = The web path the cookie represents
angularLocalStorage.constant('cookie', { expiry:30, path: '/'}); angularLocalStorage.constant('cookie', { expiry:30, path: '/'});
angularLocalStorage.constant('notify', { setItem: true, removeItem: false} );
angularLocalStorage.service('localStorageService', [ angularLocalStorage.service('localStorageService', [
'$rootScope', '$rootScope',
'prefix', 'prefix',
'cookie', 'cookie',
function($rootScope, prefix, cookie) { 'notify',
function($rootScope, prefix, cookie, notify) {
// If there is a prefix set in the config lets use that with an appended period for readability // If there is a prefix set in the config lets use that with an appended period for readability
//var prefix = angularLocalStorage.constant; //var prefix = angularLocalStorage.constant;
@ -40,6 +42,9 @@ 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');
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setItem', {key: key, newvalue: value, storageType: 'cookie'});
}
return addToCookies(key, value); return addToCookies(key, value);
} }
@ -48,6 +53,9 @@ angularLocalStorage.service('localStorageService', [
try { try {
localStorage.setItem(prefix+key, value); localStorage.setItem(prefix+key, value);
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setItem', {key: key, newvalue: value, storageType: 'localStorage'});
}
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message); $rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
return addToCookies(key, value); return addToCookies(key, value);
@ -73,11 +81,17 @@ 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');
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeItem', {key: key, storageType: 'cookie'});
}
return removeFromCookies(key); return removeFromCookies(key);
} }
try { try {
localStorage.removeItem(prefix+key); localStorage.removeItem(prefix+key);
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeItem', {key: key, storageType: 'localStorage'});
}
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message); $rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
return removeFromCookies(key); return removeFromCookies(key);