Skip to content

Commit 65896e5

Browse files
committed
"storage" event is not fired in active tab on compliant browsers. Bind to internal event instead
1 parent d19276c commit 65896e5

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

dist/angular-local-storage.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var isDefined = angular.isDefined,
1515
isObject = angular.isObject,
1616
isArray = angular.isArray,
1717
extend = angular.extend,
18-
toJson = angular.toJson;
18+
toJson = angular.toJson,
19+
fromJson = angular.toJson;
20+
1921
var angularLocalStorage = angular.module('LocalStorageModule', []);
2022

2123
angularLocalStorage.provider('localStorageService', function() {
@@ -357,7 +359,7 @@ angularLocalStorage.provider('localStorageService', function() {
357359
try {
358360
return JSON.parse(storedValues);
359361
} catch(e) {
360-
return storedValues
362+
return storedValues;
361363
}
362364
}
363365
}
@@ -401,26 +403,24 @@ angularLocalStorage.provider('localStorageService', function() {
401403
}
402404
$parse(key).assign(scope, value);
403405

404-
var onKeyUpdated = function (event) {
405-
if (event.key == deriveQualifiedKey(key)) {
406-
var updated = getFromLocalStorage(key);
407-
if(scope[key] && typeof scope[key] === "object"){
408-
angular.extend(scope[key], updated);
409-
}
410-
else {
411-
scope[key] = updated;
406+
var onKeyUpdated = function (event, data) {
407+
if (data.key === key) {
408+
scope[key] = fromJson(data.newvalue);
409+
if(!scope.$$phase) {
410+
scope.$apply();
412411
}
413-
scope.$apply();
414412
}
415413
};
416-
$window.addEventListener("storage", onKeyUpdated, false);
414+
$rootScope.$on('LocalStorageModule.notification.setitem', onKeyUpdated);
415+
$rootScope.$on('LocalStorageModule.notification.removeitem', onKeyUpdated);
417416

418417
var unregisterWatch = scope.$watch(key, function (newVal) {
419418
addToLocalStorage(lsKey, newVal);
420419
}, isObject(scope[key]));
421420
return function () {
422421
unregisterWatch();
423-
$window.removeEventListener("storage", onKeyUpdated);
422+
$rootScope.$off('LocalStorageModule.notification.setitem', onKeyUpdated);
423+
$rootScope.$off('LocalStorageModule.notification.removeitem', onKeyUpdated);
424424
};
425425
};
426426

dist/angular-local-storage.min.js

Lines changed: 1 addition & 1 deletion
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: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ angularLocalStorage.provider('localStorageService', function() {
339339
try {
340340
return JSON.parse(storedValues);
341341
} catch(e) {
342-
return storedValues
342+
return storedValues;
343343
}
344344
}
345345
}
@@ -383,26 +383,24 @@ angularLocalStorage.provider('localStorageService', function() {
383383
}
384384
$parse(key).assign(scope, value);
385385

386-
var onKeyUpdated = function (event) {
387-
if (event.key == deriveQualifiedKey(key)) {
388-
var updated = getFromLocalStorage(key);
389-
if(scope[key] && typeof scope[key] === "object"){
390-
angular.extend(scope[key], updated);
386+
var onKeyUpdated = function (event, data) {
387+
if (data.key === key) {
388+
scope[key] = fromJson(data.newvalue);
389+
if(!scope.$$phase) {
390+
scope.$apply();
391391
}
392-
else {
393-
scope[key] = updated;
394-
}
395-
scope.$apply();
396392
}
397393
};
398-
$window.addEventListener("storage", onKeyUpdated, false);
394+
$rootScope.$on('LocalStorageModule.notification.setitem', onKeyUpdated);
395+
$rootScope.$on('LocalStorageModule.notification.removeitem', onKeyUpdated);
399396

400397
var unregisterWatch = scope.$watch(key, function (newVal) {
401398
addToLocalStorage(lsKey, newVal);
402399
}, isObject(scope[key]));
403400
return function () {
404401
unregisterWatch();
405-
$window.removeEventListener("storage", onKeyUpdated);
402+
$rootScope.$off('LocalStorageModule.notification.setitem', onKeyUpdated);
403+
$rootScope.$off('LocalStorageModule.notification.removeitem', onKeyUpdated);
406404
};
407405
};
408406

src/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ var isDefined = angular.isDefined,
77
isObject = angular.isObject,
88
isArray = angular.isArray,
99
extend = angular.extend,
10-
toJson = angular.toJson;
10+
toJson = angular.toJson,
11+
fromJson = angular.toJson;

0 commit comments

Comments
 (0)