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

Better default values for @LocalStorage() Decorator #43

Closed
dopry opened this issue Apr 5, 2017 · 3 comments
Closed

Better default values for @LocalStorage() Decorator #43

dopry opened this issue Apr 5, 2017 · 3 comments

Comments

@dopry
Copy link

dopry commented Apr 5, 2017

It would be nice to be able to set the default value for properties marked up with @localStorage().

Here is an example of an ideal usage from an AuthService.

@LocalStorage('AuthService.user')		  
protected user = ANONYMOUS;

Currently, if you set user, then reload the page local storage is overwritten with anonymous on object initialization.

I wish that if 'AuthService.user' was set user would be assign to it instead of ANONYMOUS on object construction. Alternatively, it would be nice to specify the default in the Decorator if it's not possible to maintain the TS default property value semantics like @LocalStorage('AuthService.user', ANONYMOUS)

Currently we work around this in our constructor as follows,

 let storedUser = this.storage.retrieve('AuthService.user');
    if (storedUser) {
      this.user = storedUser;
    } else {
      this.user = ANONYMOUS;
    }

which is a lot of boilerplate to carry around for a default value.

@PillowPillow
Copy link
Owner

PillowPillow commented Apr 6, 2017

Hi, i agree with you, the current behavior needs a bit of boilerplate to use a default value. Unfortunately i can't determine at the decorator level if i am at the initialization step and i shouldn't override an existing value or not.

The best i could do would be add another parameter to the decorator which would be the default value.

@dopry
Copy link
Author

dopry commented Apr 6, 2017

I think a second argument to the decorator does the job, I'd love to see that come through.

@PillowPillow
Copy link
Owner

PillowPillow commented Apr 21, 2017

Hello, the last version adds the support for default value.

@LocalStorage('key', 'default value')		  
foobar;

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

No branches or pull requests

2 participants