3

I have two elements that are not directly connected (no host child relationship). I want to share some information between them as two-way data binding is not optimal. I thought about persistent session variables (like that exist in Meteor), is there is something like that in Polymer 1.0? If not, can somebody refer me to a viable solution.

Edit:

I just found out about iron-signals but the documentation states that it should be avoided, can somebody explain why it should be avoided? I would also appreciate it if somebody wrote an example of how to use it through an example.

Edit:

I discussed this question with the Polymer team in the Polymer Summit 2015. It was a part of a larger question, how to structure my app. They spoke in the summit about the mediator pattern. I recommend watching the talk from the summit and visiting the code in Github.

Salah Saleh
  • 793
  • 9
  • 29
  • https://github.com/PolymerLabs/polymer-cookie/blob/master/polymer-cookie.html cookie or a 'global' are worth look into – Robert Rowntree Aug 04 '15 at 21:28
  • For some coded examples, you might want to check out this [Stack Overflow question and its answers][1]. [1]: http://stackoverflow.com/questions/30849816/polymer-1-0-global-variables/31031678#31031678 – Let Me Tink About It Aug 07 '15 at 23:28

4 Answers4

2

I just looked at iron-signals myself recently. It is useful for communication between loosely coupled distance components. It provides an "event bus" type functionality. I believe the warning you see in the iron-signals documentation ("Note: avoid using iron-signals whenever you can use a controller (parent element) to mediate communication instead.") is to prevent over-use in simple cases where ordinary Polymer property binding would do the job. In the context of the comment included in the iron-signals documentation, the parent element (the "controller") is the binding scope for the communicating child elements.

Also, iron-signals, has nothing to do with persistent session variables. Two separate issue there.

jptknta
  • 787
  • 6
  • 15
2

I just found this video (I linked to the exact minute and second where the concept is explained) which explains exactly the effect that I want. The Key difference between the answers posted by Mowzer is that you wrap the whole of your application in a template and access the attributes through observers which is a much easier concept once you get it. I think the video is straight forward to understand, so I will not go to much explanations here.

Salah Saleh
  • 793
  • 9
  • 29
1

For some coded examples using <iron-signals>, you might want to check out this Stack Overflow question and its answers.

It's not as complicated as it first looks. Study this documentation and this demo.

It's a simple two-step process:

  1. Fire an iron-signal event like this:

    this.fire('iron-signal', {name: 'hello', data: 'kitty'});

  2. Listen for the iron-signal event with the same name (in this case, hello). Like this:

    <iron-signals on-iron-signal-hello="doSomething"></iron-signals>

Community
  • 1
  • 1
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
  • It is a bit complicated, isn't it? I actually don't like the solutions posted in that link as there is a lot of coding involved just to achieve that simple behavior. There should be a simpler solution. – Salah Saleh Aug 08 '15 at 07:44
  • It's not as complicated as it looks. [Study this documentation](https://elements.polymer-project.org/elements/iron-signals) and [this demo](https://github.com/PolymerElements/iron-signals/blob/ee879a0bd3e972b8957c1735e094cb981774de4a/demo/index.html). It's a simple two-step process. Step 1. Fire an `iron-signal` event like this: `this.fire('iron-signal', {name: 'hello', data: 'kitty'});`. Then, Step 2. Listen for the `iron-signal` event with the same name (in this case, `hello`). Like this: `` @SalahSaleh – Let Me Tink About It Aug 08 '15 at 11:04
1

In addition, here is another related question and answer with working code examples. Only they refer to <iron-meta> and <iron-local-storage>.

Community
  • 1
  • 1
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207