localStorageService.bind to return a deregistration function

master
Den Teresh 10 years ago
parent 36926d8cf5
commit 887b682815

@ -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);
});
};

@ -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');