Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto bind values in rootScope #84

Closed
bastiendonjon opened this issue Apr 8, 2014 · 11 comments
Closed

Auto bind values in rootScope #84

bastiendonjon opened this issue Apr 8, 2014 · 11 comments
Labels

Comments

@bastiendonjon
Copy link

It's possible to auto bind local storage values in the rootScope ?

@grevory
Copy link
Owner

grevory commented Apr 8, 2014

Binding things to $rootScope is generally bad practise. Try to find another
way to get the data you want like use a factory or service.
On Apr 8, 2014 1:10 PM, "bastiendonjon" notifications@github.com wrote:

It's possible to auto bind local storage values in the rootScope ?

Reply to this email directly or view it on GitHubhttps://github.com//issues/84
.

@bastiendonjon
Copy link
Author

Ok. Thanks for answer.

@Hypercubed
Copy link
Contributor

Binding to root scope might not be a good idea but what about binding any scope... something like:

localStorageService.bind($scope, 'value');

Untested proposal:

localStorageService.bind = function(scope, key) {
  scope[key] = localStorageService.get(key);

  $scope.$watch(key, function(newVal) {
    localStorageService.set(key, newVal);
  });
}

@grevory
Copy link
Owner

grevory commented Apr 17, 2014

This is a very intriguing idea.

Greg

On Thu, Apr 17, 2014 at 12:29 AM, Jayson Harshbarger <
notifications@github.com> wrote:

Binding to root scope might not be a good idea but what about binding any
scope... something like:

localStorageService.bind($scope, 'value');

Untested proposal:

localStorageService.bind = function(scope, key) {
scope[key] = localStorageService.get(key);

$scope.$watch(key, function(newVal) {
localStorageService.set(key, newVal);
});
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/84#issuecomment-40679836
.

@Hypercubed
Copy link
Contributor

I started doing this in case the default is an object. What do you think?

      localStorageService.bind = function(scope, key, def) {
        var value = localStorageService.get(key);

        if (value === null && angular.isDefined(def)) {
          value = def;
        } else if (angular.isObject(value) && angular.isObject(def)) {
          value = angular.extend(def, value);
        }

        scope[key] = value;

        scope.$watchCollection(key, function(newVal) {
          localStorageService.set(key, newVal);
        });
      };

Essentially, if both the current value and default (def) are objects, the default extends the local storage value without overwriting.

@Hypercubed
Copy link
Contributor

I can do a PR if anyone is interested.

@grevory
Copy link
Owner

grevory commented Apr 24, 2014

Sounds good. I haven't tested it yet but it looks good.

@axelsayegh
Copy link

I had a problem with .bind() inside a $scope.$on('customEvent') (the binding gets lost)
Fixed it by replacing (ln 363):

scope.$watchCollection(key, function(newVal) {
addToLocalStorage(key, newVal);
});

With:

scope.$watch(key, function (newVal) {
addToLocalStorage(key, newVal);
}, true);

Hope this is useful :)

@eddiemonge
Copy link
Collaborator

.bind() is perhaps not the best method name since JS already has one that does something.

Are you proposing having a 2 way binding with the value in $scope with the value stored locally?

@eddiemonge
Copy link
Collaborator

I would say this should be up to the app since its trivial to do there but less so to do here. Here it needs tests, testing, more code and not everyone would use/want it. You could always make a plugin that wraps this and provides that functionality.

https://github.com/grevory/angular-local-storage/pull/180/files?diff=unified#diff-e61a339ffe89f405f36274dd826711d9R27

@DaveWM
Copy link

DaveWM commented Dec 31, 2014

I've created PR #167 for 2 way binding (so the value in angular is updated when the local storage value is changed). Would someone be able to review it and hopefully get it merged please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants