[Aurelia Store] use in service class


#1

So I am attempting to incorporate aurelia-store into a project I am working on.
I have it working in standard view/viewmodel interactions, and I can subscribe an action in a service class to update values, but I can’t seem to figure out how to get access to the store values in the service class.

I am trying to retrieve for instance, the user id so that I can call the backend api to get profile information on that id.

connectTo obviously doesn’t work as there is no binding event on a plain class, and I tried the subscribing as shown in the documentation, but the state was never updated (I could navigate the store object and see the correct data was actually there when it was updated)

Anyone have a code example of how to connect up to the state values, or do I just need to locally hold those values, and just update the store from service classes?

Thanks


#2

some basic version i believe would be:


@inject(Store)
export class Service{
 @observable state;
 constructor(store) {
    store.state.subscribe(newState=>this.state);
 // or 
 store.state.pipe(
    pluck('myStateProperty'),
    distinctUntilChanged()
 ).subscribe(stateProp=>this.stateProp);
}

#3

Thanks @doktordirk for the feedback.

I was able to finally get this to work. The code example provided was failing for me.
In my case I finally figured out that the subscribe statement needed to be:

store.state.subscribe(newState=>this.state = newState);

Or

store.state.pipe(
    pluck('myStateProperty'),
    distinctUntilChanged()
 ).subscribe(stateProp=>this.stateProp = stateProp);

#4

Oh lol what a mistake from the docs. Could you create a PR for the docs fixing that?


#5

So I just did a quick look and I cannot see where to do that.
Is there a FAQ/Help file on how to submit document changes somewhere?


#6

@both that was the error in my snipet above. nothing wrong with the docs


#7

@doktordirk OK. @airboss001 in case you find something else, this is the file to modify for docs changes https://github.com/aurelia/store/blob/master/doc/article/en-US/aurelia-store-plugin.md