Saturday, September 10, 2016

data persistence across elements

Leave a Comment

suppose I want to have a data provider element for my user, like

<user-data-provider user-data="{{data}}"></user-data-provider> 

which sends an ajax request and gets the logged in user.

Suppose I want to access my user data in different pages, and I add this tag to wherever I need, but the problem is, every time the browser sees this tag, makes an ajax again and I have to wait until data is fetched!

I know I can make a variable in my main page and pass it along child pages, but that seems like overkill to me !

how can I persist user data across different pages and part of my app?

thank you in advance

2 Answers

Answers 1

There are different ways to do this.

  1. You can use use a monostate pattern
  2. You can have one instance of your user-data-provider element in your root element or index.html and use iron-signals to transmit the data to all other elements that want to consume it
  3. You can use iron-meta to have global state
  4. Use an external state management framework (i.e. redux). There is a polymer wrapper for it: polymer-redux

I would recommend using an external state mangement framework such as redux.

Some of the solutions are shown here:

Polymer 1.0 Global Variables

Answers 2

I used pouchdb for my data store, and used polymer's predefined elements to work with it

now every time that I need data, I just use that component

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment