[英]How to have reactive global variables in polymer 1.0
I followed the documentation at https://www.polymer-project.org/0.5/docs/polymer/polymer.html#global and learnt about the global variables that can be shared across all the instances of a given component. 我遵循了https://www.polymer-project.org/0.5/docs/polymer/polymer.html#global上的文档,并了解了可以在给定组件的所有实例之间共享的全局变量。 I would like to go a step further.
我想更进一步。 Would like to have the global variable to be reactive.
希望全局变量具有反应性。 Meaning if its value is changed by one of the instances, it should propagate through all other instances.
这意味着,如果实例之一更改了它的值,则它应该在所有其他实例中传播。
<dom-module is="my-writer">
<template>
<my-global user="{{username}}"></my-global>
<button on-click="changeName">Change</button>
</template>
<script>
Polymer({
is: 'my-writer',
changeName: function() { this.username = 'abhilash'; }
});
</script>
</dom-module>
<dom-module is="my-reader">
<template>
<my-global user="{{username}}"></my-global>
<span>{{username}}</span>
</template>
<script>
Polymer({
is: 'my-reader',
properties: {
username: { type: String, notify: true, value: 'goje' }
}
});
</script>
</dom-module>
<my-writer></my-writer>
<my-reader></my-reader>
This code would show a button
and a span
with value as 'goje'. 该代码将显示一个
button
和一个值为“ goje”的span
。 And when I click button, I would want the span
to change to show 'abhilash'. 当我单击按钮时,我希望
span
更改为显示“ abhilash”。
In Polymer 1.0 you may achieve something similar with iron-meta
elements. 在Polymer 1.0中,您可以使用
iron-meta
元素获得类似的效果。 These share the information stored throughout the whole webpage. 这些共享在整个网页中存储的信息。
https://elements.polymer-project.org/elements/iron-meta https://elements.polymer-project.org/elements/iron-meta
This is half way, the missing part is actually the notification/updating. 这是一半,缺少的部分实际上是通知/更新。 This actually depends on your implementation i reckon.
我认为这实际上取决于您的实现。
Further hints and various implementations can be found here: Polymer 1.0 Global Variables 在这里可以找到更多的提示和各种实现: Polymer 1.0全局变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.