grevory/angular-local-storage

No $rootScope event for different windows

monster910 opened this issue · 6 comments

I tried to watch a rootScope event between windows and it did not work

Producer method

$scope.changeStorage = function() {
  localStorageService.set('test', new Date().toISOString());
};

Consumer code

$rootScope.$on('LocalStorageModule.notification.setitem', function(oldValue, newValue) {
  console.log(oldValue);
  console.log(newValue);
});

The only way to pickup changes is in the $window listener

$window.addEventListener('storage', function (event) {
  console.log(event.key, event.newValue);
  if (event.key === 'myApp.test') {
    $timeout(function() {
      $scope.dateValue = event.newValue;
    }, 0);
  }
});

Might want to look into establishing window listeners and propagating $rootScope events between windows since $rootScope are different objects by window.

I would do this based on some preference setting.

I welcome a PR on this idea.

A PR?

Pull Request to fix it.

Actually, I think code to maintain scope data across windows should probably be handled independent of this module. It can get a bit hacky.

I'd like to see when you're done. Thanks.