Polymer 1.0: Does <iron-meta> support binding to dynamic variables?
Asked Answered
S

2

7

I can get my <iron-meta> instance to work properly when using a static value. But when I bind the value to a dynamic variable (using {{}}) it <iron-meta> no longer behaves as expected.

Does <iron-meta> support binding its value to dynamic variables?

<iron-meta id="meta" key="info" value="foo/bar"></iron-meta> // works
<iron-meta id="meta" key="info" value="{{str}}"></iron-meta> // fails
Previous work

This question is a refinement of this question in order to clarify that the ONLY thing causing the problem is the change from a static string value to a dynamic string value binding. I was getting a lot of other suggesting that had nothing to do with the change from static to dynamic so I thought it might be best to rewrite the question to clarify that. But the entire code context is contained in the links there if that would help.

Alternative solutions

There has been some recent chatter about using <iron-localstorage>. Perhaps that is the best way to go for dynamic binding essentially creating global variables?

Sapro answered 10/7, 2015 at 11:9 Comment(2)
Could you provide us the code you are using?Beckmann
@MarioLevrero: Done. See new edit.Sapro
G
11

Yes, <iron-meta> does support binding to variables, but perhaps not in the way you think.

Example: http://plnkr.co/edit/QdNepDrg9b3eCTWF6oRO?p=preview

I looked through your code here, here, and here but I'm not entirely clear what your expectations are. Hopefully my attached repro might shed some light. I see you have declaratively bound <iron-meta id="meta" key="route" xvalue="foo-bar" value="{{route}}"></iron-meta> which is fine - when route changes, iron-meta's key="route" will update accordingly.

However, be aware that in Polymer 1.0, <iron-meta> is in essence a one-way bind from parent to child in the sense that you set a meta key value dynamically by binding to a property; but to get that value, you'll have to get it imperatively via iron-meta's byKey() method.

<iron-meta> is just a simple monostate pattern implementation without an in-built path notification mechanism. What this means is value changes do not propagate upwards. Therefore, doing something like

<!-- this does not work like the way you think -->
<iron-meta id="meta" key="foo" value="{{bar}}">

in order to get the value of foo, or listen to changes to foo, does not work. This behaves more like a setter, where you set the value of foo based on your data-bound property bar.

From what I gather, it seems that you're trying to implement some sort of global variable functionality. A monostate implementation used to work in Polymer 0.5, but not in 1.0. Unfortunately, until Google endorses a "best-practice" pattern for this, suggestions till-date seems a bit speculative to me. You might find this (Polymer 1.0 Global Variables) helpful.

Gilgamesh answered 11/7, 2015 at 7:28 Comment(0)
L
4

I have had success using <iron-signals> to communicate global information. I know there is a warning in the <iron-signals> documentation that discourages its use for related elements, but when broadcasting a shared resource it seems just the thing. For example:

// source element
var db = SomeDB.init();
this.fire('iron-signal', { name: 'database', data: db });

<-- sink element -->
<iron-signals on-iron-signal-database="dbChange"></iron-signals>

class SinkElement {
  dbChange(e, detail) {
    this.db = detail;
    this.db.getSomeData();
  }
}
Lectionary answered 29/4, 2016 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.