本文介绍了聚合物1.0:< iron-meta>支持绑定到动态变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用静态值时,我可以使我的<iron-meta>实例正常工作.但是,当我将值绑定到动态变量(使用{{}})时,它的<iron-meta>不再表现出预期的行为.

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.

<iron-meta>是否支持将其值绑定到动态变量?

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.

最近有一些关于使用<iron-localstorage>的讨论.也许这是进行本质上创建全局变量的动态绑定的最佳方法?

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?

推荐答案

是的,<iron-meta>确实支持绑定到变量,但可能不支持您的思维方式.

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

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

我在这里浏览了您的代码,和此处,但我不清楚您的期望是什么.希望我附带的复制品可能会有所启发.我看到您已经声明性地绑定了<iron-meta id="meta" key="route" xvalue="foo-bar" value="{{route}}"></iron-meta>,这很好-当route更改时,iron-meta的key="route"将相应地更新.

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.

但是,请注意,在Polymer 1.0中,<iron-meta>本质上是从父项到子项的单向绑定,即您设置了元键通过绑定到属性来动态地赋值;但是要获得该值,您必须通过Iron-meta的byKey()方法强制获得它.

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>只是一个简单的 monostate模式实现,没有内置的路径通知机制.这意味着值变化不会向上传播.因此,做类似

<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}}">

为了获取 foo的值,或更改为foo,不起作用.它的行为更像 setter ,在其中您根据数据绑定属性bar设置foo的值.

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.

根据我的收集,看来您正在尝试实现某种全局变量功能.单态实现曾经在聚合物0.5中起作用,但在1.0中不起作用.不幸的是,直到Google认可了最佳实践"模式,直到现在的建议对我而言还是有点投机.您可能会发现这(聚合物1.0全局变量)很有帮助.

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.

这篇关于聚合物1.0:&lt; iron-meta&gt;支持绑定到动态变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:16