Skip to content

Commit 98fd70b

Browse files
committed
updated to binding
When local storage for a bound object is updated, it now extends the current object as opposed to overwriting it (this is to preserve unserializable properties, e.g. functions)
1 parent 9aed911 commit 98fd70b

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

dist/angular-local-storage.js

Lines changed: 8 additions & 5 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-06
3+
* @version v0.1.5 - 2014-11-27
44
* @link https://github.com/grevory/angular-local-storage
55
* @author grevory <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -407,22 +407,25 @@ angularLocalStorage.provider('localStorageService', function() {
407407
} else if (isObject(value) && isObject(def)) {
408408
value = extend(def, value);
409409
}
410-
411410
$parse(key).assign(scope, value);
412411

413412
var onKeyUpdated = function (event) {
414413
if (event.key == deriveQualifiedKey(key)) {
415-
scope[key] = getFromLocalStorage(key);
414+
var updated = getFromLocalStorage(key);
415+
if(scope[key] && typeof scope[key] === "object"){
416+
angular.extend(scope[key], updated);
417+
}
418+
else {
419+
scope[key] = updated;
420+
}
416421
scope.$apply();
417422
}
418423
};
419-
420424
$window.addEventListener("storage", onKeyUpdated, false);
421425

422426
var unregisterWatch = scope.$watch(key, function (newVal) {
423427
addToLocalStorage(lsKey, newVal);
424428
}, isObject(scope[key]));
425-
426429
return function () {
427430
unregisterWatch();
428431
$window.removeEventListener("storage", onKeyUpdated);

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,22 +380,25 @@ angularLocalStorage.provider('localStorageService', function() {
380380
} else if (isObject(value) && isObject(def)) {
381381
value = extend(def, value);
382382
}
383-
384383
$parse(key).assign(scope, value);
385384

386385
var onKeyUpdated = function (event) {
387386
if (event.key == deriveQualifiedKey(key)) {
388-
scope[key] = getFromLocalStorage(key);
387+
var updated = getFromLocalStorage(key);
388+
if(scope[key] && typeof scope[key] === "object"){
389+
angular.extend(scope[key], updated);
390+
}
391+
else {
392+
scope[key] = updated;
393+
}
389394
scope.$apply();
390395
}
391396
};
392-
393397
$window.addEventListener("storage", onKeyUpdated, false);
394398

395399
var unregisterWatch = scope.$watch(key, function (newVal) {
396400
addToLocalStorage(lsKey, newVal);
397401
}, isObject(scope[key]));
398-
399402
return function () {
400403
unregisterWatch();
401404
$window.removeEventListener("storage", onKeyUpdated);

test/spec/localStorageSpec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,27 @@ describe('localStorageService', function() {
347347
expect($rootScope.test).toEqual(testValue);
348348
}));
349349

350+
it("should keep unserializable properties of bound objects when local storage is updated", inject(function ($rootScope, localStorageService, $window){
351+
localStorageService.bind($rootScope, 'test');
352+
$rootScope.test = {obj: {a:1}, func: function (){}};
353+
// set the value in local storage mock to a value, then emit a changed event
354+
var testValue = JSON.stringify({obj:{a:2}});
355+
$window.localStorage['ls.test'] = testValue;
356+
var evt = document.createEvent('CustomEvent');
357+
evt.key = 'ls.test';
358+
evt.newValue = 'test value';
359+
evt.initCustomEvent('storage', true, true, {
360+
key: 'ls.test',
361+
newValue: testValue
362+
});
363+
window.dispatchEvent(evt);
364+
$rootScope.$digest();
365+
366+
expect($rootScope.test.obj).toEqual({a:2});
367+
expect($rootScope.test.func).toBeDefined();
368+
expect(typeof $rootScope.test.func).toBe('function');
369+
}));
370+
350371
it('should be able to bind to scope using different key', inject(function($rootScope, localStorageService) {
351372

352373
localStorageService.set('lsProperty', 'oldValue');

0 commit comments

Comments
 (0)