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

梦想与她 提交于 2019-12-05 04:46:41
zerodevx

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.

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();
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!