Skip to content

Commit 9aed911

Browse files
committed
Listen for change event for bound values
When you call localStorageService.bind, an event listener is now registered that is fired when the value of the local storage key changes, which updates the value and calls scope.$apply().
1 parent 3c5e2ec commit 9aed911

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

dist/angular-local-storage.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* An Angular module that gives you access to the browsers local storage
3-
* @version v0.1.5 - 2014-11-04
3+
* @version v0.1.5 - 2014-11-06
44
* @link https://github.com/grevory/angular-local-storage
55
* @author grevory <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -181,7 +181,6 @@ angularLocalStorage.provider('localStorageService', function() {
181181
// Directly get a value from local storage
182182
// Example use: localStorageService.get('library'); // returns 'angular'
183183
var getFromLocalStorage = function (key) {
184-
185184
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
186185
if (!browserSupportsLocalStorage) {
187186
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
@@ -411,9 +410,23 @@ angularLocalStorage.provider('localStorageService', function() {
411410

412411
$parse(key).assign(scope, value);
413412

414-
return scope.$watch(key, function(newVal) {
413+
var onKeyUpdated = function (event) {
414+
if (event.key == deriveQualifiedKey(key)) {
415+
scope[key] = getFromLocalStorage(key);
416+
scope.$apply();
417+
}
418+
};
419+
420+
$window.addEventListener("storage", onKeyUpdated, false);
421+
422+
var unregisterWatch = scope.$watch(key, function (newVal) {
415423
addToLocalStorage(lsKey, newVal);
416424
}, isObject(scope[key]));
425+
426+
return function () {
427+
unregisterWatch();
428+
$window.removeEventListener("storage", onKeyUpdated);
429+
};
417430
};
418431

419432
// Return localStorageService.length

dist/angular-local-storage.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-local-storage.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ angularLocalStorage.provider('localStorageService', function() {
154154
// Directly get a value from local storage
155155
// Example use: localStorageService.get('library'); // returns 'angular'
156156
var getFromLocalStorage = function (key) {
157-
158157
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
159158
if (!browserSupportsLocalStorage) {
160159
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
@@ -384,9 +383,23 @@ angularLocalStorage.provider('localStorageService', function() {
384383

385384
$parse(key).assign(scope, value);
386385

387-
return scope.$watch(key, function(newVal) {
386+
var onKeyUpdated = function (event) {
387+
if (event.key == deriveQualifiedKey(key)) {
388+
scope[key] = getFromLocalStorage(key);
389+
scope.$apply();
390+
}
391+
};
392+
393+
$window.addEventListener("storage", onKeyUpdated, false);
394+
395+
var unregisterWatch = scope.$watch(key, function (newVal) {
388396
addToLocalStorage(lsKey, newVal);
389397
}, isObject(scope[key]));
398+
399+
return function () {
400+
unregisterWatch();
401+
$window.removeEventListener("storage", onKeyUpdated);
402+
};
390403
};
391404

392405
// Return localStorageService.length

test/spec/localStorageSpec.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,16 @@ describe('localStorageService', function() {
117117
}
118118

119119
beforeEach(module('LocalStorageModule', function($provide) {
120-
120+
spyOn(window, 'addEventListener').andCallThrough();
121+
spyOn(window, 'removeEventListener').andCallThrough();
121122
$provide.value('$window', {
122-
localStorage: localStorageMock()
123+
localStorage: localStorageMock(),
124+
addEventListener: function () {
125+
window.addEventListener.apply(window, arguments);
126+
},
127+
removeEventListener : function (){
128+
window.removeEventListener.apply(window, arguments);
129+
}
123130
});
124131

125132
}));
@@ -286,6 +293,7 @@ describe('localStorageService', function() {
286293
$rootScope.$digest();
287294

288295
expect($rootScope.property).toEqual(localStorageService.get('property'));
296+
expect(window.addEventListener).toHaveBeenCalled();
289297
}));
290298

291299
it('should be able to unbind from scope variable', inject(function($rootScope, localStorageService) {
@@ -303,6 +311,7 @@ describe('localStorageService', function() {
303311
$rootScope.$digest();
304312

305313
expect($rootScope.property).not.toEqual(localStorageService.get('property'));
314+
expect(window.removeEventListener).toHaveBeenCalled();
306315
}));
307316

308317
it('should be able to bind to properties of objects', inject(function($rootScope, localStorageService) {
@@ -318,6 +327,26 @@ describe('localStorageService', function() {
318327
expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property'));
319328
}));
320329

330+
it('should update a bound value when local storage is updated', inject(function ($rootScope, localStorageService, $window){
331+
localStorageService.bind($rootScope, 'test');
332+
$rootScope.$digest();
333+
334+
// set the value in local storage mock to a value, then emit a changed event
335+
var testValue = 'test';
336+
$window.localStorage['ls.test'] = testValue;
337+
var evt = document.createEvent('CustomEvent');
338+
evt.key = 'ls.test';
339+
evt.newValue = 'test value';
340+
evt.initCustomEvent('storage', true, true, {
341+
key: 'ls.test',
342+
newValue: testValue
343+
});
344+
window.dispatchEvent(evt);
345+
$rootScope.$digest();
346+
347+
expect($rootScope.test).toEqual(testValue);
348+
}));
349+
321350
it('should be able to bind to scope using different key', inject(function($rootScope, localStorageService) {
322351

323352
localStorageService.set('lsProperty', 'oldValue');
@@ -329,6 +358,7 @@ describe('localStorageService', function() {
329358
$rootScope.$digest();
330359

331360
expect($rootScope.property).toEqual(localStorageService.get('lsProperty'));
361+
expect(window.addEventListener).toHaveBeenCalled();
332362
}));
333363

334364
it('should $watch with deep comparison only for objects', inject(function($rootScope, localStorageService) {

0 commit comments

Comments
 (0)