grevory / angular-local-storage

An AngularJS module that gives you access to the browsers local storage with cookie fallback
Other
2.83k stars 585 forks source link

No $rootScope event for different windows #345

Closed monster910 closed 7 years ago

monster910 commented 7 years ago

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.

grevory commented 7 years ago

I welcome a PR on this idea.

monster910 commented 7 years ago

A PR?

grevory commented 7 years ago

Pull Request to fix it.

grevory commented 7 years ago

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

monster910 commented 7 years ago

I have a fork. I will see what I can do. Maybe the best thing is an example test case for folks..

On Wed, Apr 19, 2017 at 3:08 PM, Gregory Pike notifications@github.com wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/grevory/angular-local-storage/issues/345#issuecomment-295419041, or mute the thread https://github.com/notifications/unsubscribe-auth/AAwcdEvZ37bEbyj-I_hjb03TyABbygMDks5rxmnWgaJpZM4MW4-A .

grevory commented 7 years ago

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