From 255fafe769abc42d783d0fd4385df3972948149c Mon Sep 17 00:00:00 2001 From: lucuma Date: Mon, 22 Apr 2013 16:30:48 -0500 Subject: [PATCH 1/2] Update localStorageModule.js Configurable notifications of add and remove items. --- localStorageModule.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/localStorageModule.js b/localStorageModule.js index b51e117..2e7428a 100644 --- a/localStorageModule.js +++ b/localStorageModule.js @@ -9,12 +9,14 @@ angularLocalStorage.constant('prefix', 'ls'); // expiry = Number of days before cookies expire // 0 = Does not expire // path = The web path the cookie represents angularLocalStorage.constant('cookie', { expiry:30, path: '/'}); +angularLocalStorage.constant('notify', { setItem: true, removeItem: false} ); angularLocalStorage.service('localStorageService', [ '$rootScope', 'prefix', '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 //var prefix = angularLocalStorage.constant; @@ -40,6 +42,9 @@ angularLocalStorage.service('localStorageService', [ // If this browser does not support local storage use cookies if (!browserSupportsLocalStorage()) { $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); } @@ -48,6 +53,9 @@ angularLocalStorage.service('localStorageService', [ try { localStorage.setItem(prefix+key, value); + if (notify.setItem) { + $rootScope.$broadcast('LocalStorageModule.notification.setItem', {key: key, newvalue: value, storageType: 'localStorage'}); + } } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message); return addToCookies(key, value); @@ -73,11 +81,17 @@ angularLocalStorage.service('localStorageService', [ var removeFromLocalStorage = function (key) { if (!browserSupportsLocalStorage()) { $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); + if (notify.removeItem) { + $rootScope.$broadcast('LocalStorageModule.notification.removeItem', {key: key, storageType: 'cookie'}); + } return removeFromCookies(key); } try { localStorage.removeItem(prefix+key); + if (notify.removeItem) { + $rootScope.$broadcast('LocalStorageModule.notification.removeItem', {key: key, storageType: 'localStorage'}); + } } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message); return removeFromCookies(key); From 035a10b71509f6ab27e862007e5c3c1d8123af2c Mon Sep 17 00:00:00 2001 From: lucuma Date: Mon, 22 Apr 2013 16:39:45 -0500 Subject: [PATCH 2/2] Update localStorageModule.js Change notifications to be consistent with original code, lowercase. --- localStorageModule.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/localStorageModule.js b/localStorageModule.js index 2e7428a..ca23c51 100644 --- a/localStorageModule.js +++ b/localStorageModule.js @@ -43,7 +43,7 @@ angularLocalStorage.service('localStorageService', [ if (!browserSupportsLocalStorage()) { $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); if (notify.setItem) { - $rootScope.$broadcast('LocalStorageModule.notification.setItem', {key: key, newvalue: value, storageType: 'cookie'}); + $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'}); } return addToCookies(key, value); } @@ -54,7 +54,7 @@ angularLocalStorage.service('localStorageService', [ try { localStorage.setItem(prefix+key, value); if (notify.setItem) { - $rootScope.$broadcast('LocalStorageModule.notification.setItem', {key: key, newvalue: value, storageType: 'localStorage'}); + $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'localStorage'}); } } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message); @@ -82,7 +82,7 @@ angularLocalStorage.service('localStorageService', [ if (!browserSupportsLocalStorage()) { $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); if (notify.removeItem) { - $rootScope.$broadcast('LocalStorageModule.notification.removeItem', {key: key, storageType: 'cookie'}); + $rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'cookie'}); } return removeFromCookies(key); } @@ -90,7 +90,7 @@ angularLocalStorage.service('localStorageService', [ try { localStorage.removeItem(prefix+key); if (notify.removeItem) { - $rootScope.$broadcast('LocalStorageModule.notification.removeItem', {key: key, storageType: 'localStorage'}); + $rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'localStorage'}); } } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message);