diff --git a/README.md b/README.md index 011681f..20f3bd3 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ myApp.config(function (localStorageServiceProvider) { .setPrefix('yourAppName'); }); ``` +<<<<<<< HEAD ###setStorageType You could change web storage type to localStorage or sessionStorage
**Default storage:** `localStorage` @@ -217,16 +218,21 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { ###bind Bind $scope key to localStorageService. **Usage:** `localStorageService.bind(scope, property, value[optional], key[optional])` -***key:*** The corresponding key used in local storage +***key:*** The corresponding key used in local storage +**Returns:** deregistration function for this listener. ```js myApp.controller('MainCtrl', function($scope, localStorageService) { //... localStorageService.set('property', 'oldValue'); - localStorageService.bind($scope, 'property'); + var unbind = localStorageService.bind($scope, 'property'); //Test Changes - $scope.property = 'newValue'; - console.log(localStorageService.get('property')) // newValue; + $scope.property = 'newValue1'; + console.log(localStorageService.get('property')) // newValue1; + //unbind watcher + unbind(); + $scope.property = 'newValue2'; + console.log(localStorageService.get('property')) // newValue1; //... }); ``` @@ -302,7 +308,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` -Check out the full demo and documentation at http://gregpike.net/demos/angular-local-storage/demo.html +Check out the full demo at http://gregpike.net/demos/angular-local-storage/demo.html ##TO DO: - Expand Readme diff --git a/src/angular-local-storage.js b/src/angular-local-storage.js index 9b2e58e..6b00fec 100644 --- a/src/angular-local-storage.js +++ b/src/angular-local-storage.js @@ -365,6 +365,8 @@ angularLocalStorage.provider('localStorageService', function() { return storageType; }; + // Add a listener on scope variable to save its changes to local storage + // Return a function which when called cancels binding var bindToScope = function(scope, scopeKey, def, lsKey) { if (!lsKey) { lsKey = scopeKey; @@ -380,7 +382,7 @@ angularLocalStorage.provider('localStorageService', function() { $parse(scopeKey).assign(scope, value); - scope.$watchCollection(scopeKey, function(newVal) { + return scope.$watchCollection(scopeKey, function(newVal) { addToLocalStorage(lsKey, newVal); }); }; diff --git a/test/spec/localStorageSpec.js b/test/spec/localStorageSpec.js index adfe4d4..14551c5 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -277,6 +277,23 @@ describe('localStorageService', function() { expect($rootScope.property).toEqual(localStorageService.get('property')); })); + it('should be able to unbind from scope variable', inject(function($rootScope, localStorageService) { + + localStorageService.set('property', 'oldValue'); + var lsUnbind = localStorageService.bind($rootScope, 'property'); + + $rootScope.property = 'newValue'; + $rootScope.$digest(); + + expect($rootScope.property).toEqual(localStorageService.get('property')); + + lsUnbind(); + $rootScope.property = 'anotherValue'; + $rootScope.$digest(); + + expect($rootScope.property).not.toEqual(localStorageService.get('property')); + })); + it('should be able to bind to properties of objects', inject(function($rootScope, localStorageService) { localStorageService.set('obj.property', 'oldValue');