Merge pull request #124 from yihangho/binding

Improve binding
This commit is contained in:
Gregory Pike 2014-10-03 00:45:57 -02:30
commit 7d86e3321c
3 changed files with 39 additions and 9 deletions

View File

@ -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

View File

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

View File

@ -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() {