fix(localStorageService): fix usage of 'this' keyword on closures

master
Ariel Mashraki 10 years ago
parent b29b89676d
commit 9e3537bfd5

@ -4,7 +4,7 @@
var angularLocalStorage = angular.module('LocalStorageModule', []); var angularLocalStorage = angular.module('LocalStorageModule', []);
angularLocalStorage.provider('localStorageService', function() { angularLocalStorage.provider('localStorageService', function() {
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app // You should set a prefix to avoid overwriting any local storage variables from the rest of your app
// e.g. localStorageServiceProvider.setPrefix('youAppName'); // e.g. localStorageServiceProvider.setPrefix('youAppName');
// With provider you can use config as this: // With provider you can use config as this:
@ -65,11 +65,11 @@ angularLocalStorage.provider('localStorageService', function() {
this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) { this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) {
var self = this;
var prefix = this.prefix; var prefix = self.prefix;
var cookie = this.cookie; var cookie = self.cookie;
var notify = this.notify; var notify = self.notify;
var storageType = this.storageType; var storageType = self.storageType;
var webStorage; var webStorage;
// When Angular's $document is not available // When Angular's $document is not available
@ -110,7 +110,7 @@ angularLocalStorage.provider('localStorageService', function() {
return false; return false;
} }
}()); }());
// Directly adds a value to local storage // Directly adds a value to local storage
@ -119,7 +119,7 @@ angularLocalStorage.provider('localStorageService', function() {
var addToLocalStorage = function (key, value) { var addToLocalStorage = function (key, value) {
// If this browser does not support local storage use cookies // If this browser does not support local storage use cookies
if (!browserSupportsLocalStorage || this.storageType === 'cookie') { if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
if (notify.setItem) { 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'});
@ -138,7 +138,7 @@ angularLocalStorage.provider('localStorageService', function() {
} }
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)}; if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
if (notify.setItem) { if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: this.storageType}); $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
} }
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message); $rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
@ -151,7 +151,7 @@ angularLocalStorage.provider('localStorageService', function() {
// Example use: localStorageService.get('library'); // returns 'angular' // Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) { var getFromLocalStorage = function (key) {
if (!browserSupportsLocalStorage || this.storageType === 'cookie') { if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
return getFromCookies(key); return getFromCookies(key);
} }
@ -184,7 +184,7 @@ angularLocalStorage.provider('localStorageService', function() {
try { try {
webStorage.removeItem(deriveQualifiedKey(key)); webStorage.removeItem(deriveQualifiedKey(key));
if (notify.removeItem) { if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: this.storageType}); $rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: self.storageType});
} }
} catch (e) { } catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message); $rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
@ -335,7 +335,7 @@ angularLocalStorage.provider('localStorageService', function() {
var cookies = $document.cookie.split(';'); var cookies = $document.cookie.split(';');
for(var i = 0; i < cookies.length; i++) { for(var i = 0; i < cookies.length; i++) {
thisCookie = cookies[i]; thisCookie = cookies[i];
while (thisCookie.charAt(0) === ' ') { while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1, thisCookie.length); thisCookie = thisCookie.substring(1, thisCookie.length);
} }