Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Access to state in state aware element #15

Closed
RobMaskell opened this issue Oct 12, 2017 · 3 comments
Closed

Access to state in state aware element #15

RobMaskell opened this issue Oct 12, 2017 · 3 comments

Comments

@RobMaskell
Copy link

I'm trying to access state in javascript, all the examples I can find for state-aware show accessing state in the template. I can access this.stateAware and this.statePath ok but not the state itself. I'm using the latest 0.6.0 release.

What I'm trying to do is store some application level state to be used for many pages (the HATEOS route of my api which is pulled from the server) so I wanted to store it in state and access it using state-aware without passing it down through the element hierarchy. Is what I'm doing sensible or is there a better way, maybe the application object that I don't really understand what it's for?

Thanks for the help in advance, great project if I can work out how to use it properly.

@eterna2
Copy link

eterna2 commented Oct 15, 2017

You can use get

this.get('state.someEle.someField')

Similarly, you can use set to set the state

this.set('state.someEle.someField', someValue)

@eterna2
Copy link

eterna2 commented Oct 15, 2017

Just a note, if your path is more than 1 level deep, you will need to create the Object first as it will not be automatically created.

this.set('state.level1.level2', true)

If state.level1 does not exist, nothing will be set.

@RobMaskell
Copy link
Author

Thanks, that works a treat. I was trying to use this.state which seems to work sometimes but not always even in the todo app

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

No branches or pull requests

2 participants