diff --git a/README.md b/README.md index 02fb952..2c45208 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/angular-local-storage.js b/angular-local-storage.js index f212b16..8e02988 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; @@ -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); } - scope[key] = 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 2e7f8c7..7ca0042 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -236,6 +236,32 @@ 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')); + })); + + 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() {