Skip to content

Commit 3f3cd06

Browse files
committed
Merge branch 'den-t-master'
2 parents 6d763a7 + 45fd4c9 commit 3f3cd06

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ myApp.config(function (localStorageServiceProvider) {
7878
.setPrefix('yourAppName');
7979
});
8080
```
81+
<<<<<<< HEAD
8182
###setStorageType
8283
You could change web storage type to localStorage or sessionStorage<br/>
8384
**Default storage:** `localStorage`
@@ -217,16 +218,21 @@ myApp.controller('MainCtrl', function($scope, localStorageService) {
217218
###bind
218219
Bind $scope key to localStorageService.
219220
**Usage:** `localStorageService.bind(scope, property, value[optional], key[optional])`
220-
***key:*** The corresponding key used in local storage
221+
***key:*** The corresponding key used in local storage
222+
**Returns:** deregistration function for this listener.
221223
```js
222224
myApp.controller('MainCtrl', function($scope, localStorageService) {
223225
//...
224226
localStorageService.set('property', 'oldValue');
225-
localStorageService.bind($scope, 'property');
227+
var unbind = localStorageService.bind($scope, 'property');
226228

227229
//Test Changes
228-
$scope.property = 'newValue';
229-
console.log(localStorageService.get('property')) // newValue;
230+
$scope.property = 'newValue1';
231+
console.log(localStorageService.get('property')) // newValue1;
232+
//unbind watcher
233+
unbind();
234+
$scope.property = 'newValue2';
235+
console.log(localStorageService.get('property')) // newValue1;
230236
//...
231237
});
232238
```
@@ -302,7 +308,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) {
302308
});
303309
```
304310

305-
Check out the full demo and documentation at http://gregpike.net/demos/angular-local-storage/demo.html
311+
Check out the full demo at http://gregpike.net/demos/angular-local-storage/demo.html
306312

307313
##TO DO:
308314
- Expand Readme

src/angular-local-storage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ angularLocalStorage.provider('localStorageService', function() {
365365
return storageType;
366366
};
367367

368+
// Add a listener on scope variable to save its changes to local storage
369+
// Return a function which when called cancels binding
368370
var bindToScope = function(scope, scopeKey, def, lsKey) {
369371
if (!lsKey) {
370372
lsKey = scopeKey;
@@ -380,7 +382,7 @@ angularLocalStorage.provider('localStorageService', function() {
380382

381383
$parse(scopeKey).assign(scope, value);
382384

383-
scope.$watchCollection(scopeKey, function(newVal) {
385+
return scope.$watchCollection(scopeKey, function(newVal) {
384386
addToLocalStorage(lsKey, newVal);
385387
});
386388
};

test/spec/localStorageSpec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,23 @@ describe('localStorageService', function() {
277277
expect($rootScope.property).toEqual(localStorageService.get('property'));
278278
}));
279279

280+
it('should be able to unbind from scope variable', inject(function($rootScope, localStorageService) {
281+
282+
localStorageService.set('property', 'oldValue');
283+
var lsUnbind = localStorageService.bind($rootScope, 'property');
284+
285+
$rootScope.property = 'newValue';
286+
$rootScope.$digest();
287+
288+
expect($rootScope.property).toEqual(localStorageService.get('property'));
289+
290+
lsUnbind();
291+
$rootScope.property = 'anotherValue';
292+
$rootScope.$digest();
293+
294+
expect($rootScope.property).not.toEqual(localStorageService.get('property'));
295+
}));
296+
280297
it('should be able to bind to properties of objects', inject(function($rootScope, localStorageService) {
281298

282299
localStorageService.set('obj.property', 'oldValue');

0 commit comments

Comments
 (0)