From 9e3537bfd513b23cfb184ab93b5d07183a365dc4 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Thu, 14 Aug 2014 08:55:24 +0300 Subject: [PATCH] fix(localStorageService): fix usage of 'this' keyword on closures --- angular-local-storage.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/angular-local-storage.js b/angular-local-storage.js index d60b122..af4f8ce 100644 --- a/angular-local-storage.js +++ b/angular-local-storage.js @@ -4,7 +4,7 @@ var angularLocalStorage = angular.module('LocalStorageModule', []); angularLocalStorage.provider('localStorageService', function() { - + // You should set a prefix to avoid overwriting any local storage variables from the rest of your app // e.g. localStorageServiceProvider.setPrefix('youAppName'); // 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) { - - var prefix = this.prefix; - var cookie = this.cookie; - var notify = this.notify; - var storageType = this.storageType; + var self = this; + var prefix = self.prefix; + var cookie = self.cookie; + var notify = self.notify; + var storageType = self.storageType; var webStorage; // When Angular's $document is not available @@ -110,7 +110,7 @@ angularLocalStorage.provider('localStorageService', function() { return false; } }()); - + // Directly adds a value to local storage @@ -119,7 +119,7 @@ angularLocalStorage.provider('localStorageService', function() { var addToLocalStorage = function (key, value) { // 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'); if (notify.setItem) { $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 (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) { $rootScope.$broadcast('LocalStorageModule.notification.error', e.message); @@ -151,7 +151,7 @@ angularLocalStorage.provider('localStorageService', function() { // Example use: localStorageService.get('library'); // returns 'angular' var getFromLocalStorage = function (key) { - if (!browserSupportsLocalStorage || this.storageType === 'cookie') { + if (!browserSupportsLocalStorage || self.storageType === 'cookie') { $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); return getFromCookies(key); } @@ -184,7 +184,7 @@ angularLocalStorage.provider('localStorageService', function() { try { webStorage.removeItem(deriveQualifiedKey(key)); 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) { $rootScope.$broadcast('LocalStorageModule.notification.error', e.message); @@ -335,7 +335,7 @@ angularLocalStorage.provider('localStorageService', function() { var cookies = $document.cookie.split(';'); for(var i = 0; i < cookies.length; i++) { thisCookie = cookies[i]; - + while (thisCookie.charAt(0) === ' ') { thisCookie = thisCookie.substring(1, thisCookie.length); }