From 84a9b404b6ac52eabc26fff3875a983dc3e13742 Mon Sep 17 00:00:00 2001 From: Yihang Ho Date: Sat, 23 Aug 2014 21:50:46 +0800 Subject: [PATCH 1/3] Bind into property of objects. --- angular-local-storage.js | 4 ++-- test/spec/localStorageSpec.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/angular-local-storage.js b/angular-local-storage.js index f212b16..5f83a1a 100644 --- a/angular-local-storage.js +++ b/angular-local-storage.js @@ -62,7 +62,7 @@ angularLocalStorage.provider('localStorageService', function() { }; }; - this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) { + this.$get = ['$rootScope', '$window', '$document', '$parse', function($rootScope, $window, $document, $parse) { var self = this; var prefix = self.prefix; var cookie = self.cookie; @@ -368,7 +368,7 @@ angularLocalStorage.provider('localStorageService', function() { value = angular.extend(def, value); } - scope[key] = value; + $parse(key).assign(scope, value); scope.$watchCollection(key, function(newVal) { addToLocalStorage(key, newVal); diff --git a/test/spec/localStorageSpec.js b/test/spec/localStorageSpec.js index 2e7f8c7..20f52de 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -236,6 +236,19 @@ describe('localStorageService', function() { expect($rootScope.property).toEqual(localStorageService.get('property')); })); + it('should be able to bind to properties of objects', inject(function($rootScope, localStorageService) { + + localStorageService.set('obj.property', 'oldValue'); + localStorageService.bind($rootScope, 'obj.property'); + + expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property')); + + $rootScope.obj.property = 'newValue'; + $rootScope.$digest(); + + expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property')); + })); + //sessionStorage describe('SessionStorage', function() { From 0b8d7820e91ee6508475e84e26fc3a5a07ae710e Mon Sep 17 00:00:00 2001 From: Yihang Ho Date: Sat, 23 Aug 2014 21:58:00 +0800 Subject: [PATCH 2/3] Able to bind to scope using different keys. --- angular-local-storage.js | 14 +++++++++----- test/spec/localStorageSpec.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/angular-local-storage.js b/angular-local-storage.js index 5f83a1a..8e02988 100644 --- a/angular-local-storage.js +++ b/angular-local-storage.js @@ -359,8 +359,12 @@ angularLocalStorage.provider('localStorageService', function() { return storageType; }; - var bindToScope = function(scope, key, def) { - var value = getFromLocalStorage(key); + var bindToScope = function(scope, scopeKey, def, lsKey) { + if (!lsKey) { + lsKey = scopeKey; + } + + var value = getFromLocalStorage(lsKey); if (value === null && angular.isDefined(def)) { value = def; @@ -368,10 +372,10 @@ angularLocalStorage.provider('localStorageService', function() { value = angular.extend(def, value); } - $parse(key).assign(scope, value); + $parse(scopeKey).assign(scope, value); - scope.$watchCollection(key, function(newVal) { - addToLocalStorage(key, newVal); + scope.$watchCollection(scopeKey, function(newVal) { + addToLocalStorage(lsKey, newVal); }); }; diff --git a/test/spec/localStorageSpec.js b/test/spec/localStorageSpec.js index 20f52de..7ca0042 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -249,6 +249,19 @@ describe('localStorageService', function() { expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property')); })); + it('should be able to bind to scope using different key', inject(function($rootScope, localStorageService) { + + localStorageService.set('lsProperty', 'oldValue'); + localStorageService.bind($rootScope, 'property', undefined, 'lsProperty'); + + expect($rootScope.property).toEqual(localStorageService.get('lsProperty')); + + $rootScope.property = 'newValue'; + $rootScope.$digest(); + + expect($rootScope.property).toEqual(localStorageService.get('lsProperty')); + })); + //sessionStorage describe('SessionStorage', function() { From 0ff995d63b62780f92a695b930d81eee912d9690 Mon Sep 17 00:00:00 2001 From: Yihang Ho Date: Sat, 23 Aug 2014 22:02:39 +0800 Subject: [PATCH 3/3] Update README. --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 04506b4..23bf564 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Installation: bower install angular-local-storage ``` -Example use: +Example use: ```javascript angular.module('yourModule', ['LocalStorageModule']) @@ -21,16 +21,16 @@ angular.module('yourModule', ['LocalStorageModule']) function($scope, localStorageService) { // Start fresh localStorageService.clearAll(); - + // Set a key localStorageService.set('Favorite Sport','Ultimate Frisbee'); - + // Delete a key localStorageService.delete('Favorite Sport'); }]); /* -To set the prefix of your localStorage name, you can use the setPrefix method +To set the prefix of your localStorage name, you can use the setPrefix method available on the localStorageServiceProvider */ angular.module('yourModule', ['LocalStorageModule']) @@ -40,16 +40,16 @@ angular.module('yourModule', ['LocalStorageModule']) ``` #### How to bind to a $scope variable: -Usage: localStorageService.bind(scope, key, def); +Usage: localStorageService.bind(scope, scopeKey, def, lsKey); ``` // Example $scope.anArtist = {'firstname':'Pablo', 'lastname':'Picasso'}; // Bind to local storage service -localStorageService.bind($scope, 'anArtist', anArtist); +localStorageService.bind($scope, 'anArtist', $scope.anArtist, 'specialArtist'); // get bound data: -console.log(localStorageService.get('anArtist')); +console.log(localStorageService.get('specialArtist')); ``` Check out the full demo and documentation at http://gregpike.net/demos/angular-local-storage/demo.html