Skip to content

Commit

Permalink
"storage" event is not fired in active tab on compliant browsers. Bin…
Browse files Browse the repository at this point in the history
…d to internal event instead
  • Loading branch information
STRML committed Jul 15, 2015
1 parent d19276c commit 65896e5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
26 changes: 13 additions & 13 deletions dist/angular-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ var isDefined = angular.isDefined,
isObject = angular.isObject,
isArray = angular.isArray,
extend = angular.extend,
toJson = angular.toJson;
toJson = angular.toJson,
fromJson = angular.toJson;

var angularLocalStorage = angular.module('LocalStorageModule', []);

angularLocalStorage.provider('localStorageService', function() {
Expand Down Expand Up @@ -357,7 +359,7 @@ angularLocalStorage.provider('localStorageService', function() {
try {
return JSON.parse(storedValues);
} catch(e) {
return storedValues
return storedValues;
}
}
}
Expand Down Expand Up @@ -401,26 +403,24 @@ angularLocalStorage.provider('localStorageService', function() {
}
$parse(key).assign(scope, value);

var onKeyUpdated = function (event) {
if (event.key == deriveQualifiedKey(key)) {
var updated = getFromLocalStorage(key);
if(scope[key] && typeof scope[key] === "object"){
angular.extend(scope[key], updated);
}
else {
scope[key] = updated;
var onKeyUpdated = function (event, data) {
if (data.key === key) {
scope[key] = fromJson(data.newvalue);
if(!scope.$$phase) {
scope.$apply();
}
scope.$apply();
}
};
$window.addEventListener("storage", onKeyUpdated, false);
$rootScope.$on('LocalStorageModule.notification.setitem', onKeyUpdated);
$rootScope.$on('LocalStorageModule.notification.removeitem', onKeyUpdated);

var unregisterWatch = scope.$watch(key, function (newVal) {
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
return function () {
unregisterWatch();
$window.removeEventListener("storage", onKeyUpdated);
$rootScope.$off('LocalStorageModule.notification.setitem', onKeyUpdated);
$rootScope.$off('LocalStorageModule.notification.removeitem', onKeyUpdated);
};
};

Expand Down
2 changes: 1 addition & 1 deletion dist/angular-local-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions src/angular-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ angularLocalStorage.provider('localStorageService', function() {
try {
return JSON.parse(storedValues);
} catch(e) {
return storedValues
return storedValues;
}
}
}
Expand Down Expand Up @@ -383,26 +383,24 @@ angularLocalStorage.provider('localStorageService', function() {
}
$parse(key).assign(scope, value);

var onKeyUpdated = function (event) {
if (event.key == deriveQualifiedKey(key)) {
var updated = getFromLocalStorage(key);
if(scope[key] && typeof scope[key] === "object"){
angular.extend(scope[key], updated);
var onKeyUpdated = function (event, data) {
if (data.key === key) {
scope[key] = fromJson(data.newvalue);
if(!scope.$$phase) {
scope.$apply();
}
else {
scope[key] = updated;
}
scope.$apply();
}
};
$window.addEventListener("storage", onKeyUpdated, false);
$rootScope.$on('LocalStorageModule.notification.setitem', onKeyUpdated);
$rootScope.$on('LocalStorageModule.notification.removeitem', onKeyUpdated);

var unregisterWatch = scope.$watch(key, function (newVal) {
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
return function () {
unregisterWatch();
$window.removeEventListener("storage", onKeyUpdated);
$rootScope.$off('LocalStorageModule.notification.setitem', onKeyUpdated);
$rootScope.$off('LocalStorageModule.notification.removeitem', onKeyUpdated);
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ var isDefined = angular.isDefined,
isObject = angular.isObject,
isArray = angular.isArray,
extend = angular.extend,
toJson = angular.toJson;
toJson = angular.toJson,
fromJson = angular.toJson;

0 comments on commit 65896e5

Please sign in to comment.