简体   繁体   English

在聚合物2.x中使用铁元元素来管理状态

[英]Using iron-meta to manage state in Polymer 2.x

I am trying to use iron-meta to manage state in a Polymer app. 我正在尝试使用iron-meta来管理Polymer应用程序中的状态。 Here is my Plunker demo. 这是我的Plunker演示。

On that same Plunker, I am also using Mixins to manage state. 在同一个Plunker上,我还使用Mixins来管理状态。 (So we can compare the two systems.) (因此,我们可以比较两个系统。)

Desired Behavior 期望的行为

On the demo, at the bottom, I expect the part that follows "Meta says:" to match and track the part that follows "Send notifications option is:" when the options checkbox at the top labeled "Send Notifications" is clicked. 在演示的底部,我希望单击顶部标记为“发送通知”的选项复选框时,跟在“ Meta说:”后面的部分可以匹配并跟踪“发送通知选项是:”后面的部分。

Actual Behavior 实际行为

But instead of that desired behavior, the actual behavior I get is the "Meta says:" section never populates with any data. 但是我得到的实际行为不是“期望的行为”,而是“元数据说:”部分从不填充任何数据。

Demo 演示

Plunker Demo 柱塞演示

Demo screen recording 演示画面录制

铁元演示

my-view2.html 我-view2.html
 <link rel="import" href="my-options.html"> <base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/"> <link rel="import" href="polymer/polymer-element.html"> <link rel="import" href="iron-meta/iron-meta.html"> <dom-module id="my-view2"> <template> <style> :host { display: block; padding: 10px; } </style> <iron-meta key="meta" value="{{meta}}"></iron-meta> <my-options></my-options> <div class="card"> <div class="circle">2</div> <h1>View Two</h1> <p>Ea duis bonorum nec, falli paulo aliquid ei eum.</p> <p>Id nam odio natum malorum, tibique copiosae expetenda mel ea.Detracto suavitate repudiandae no eum. Id adhuc minim soluta nam.Id nam odio natum malorum, tibique copiosae expetenda mel ea.</p> <p>Send notifications option is: <b>[[ options.subscribe ]]</b></p> <p>Meta says: <b>[[ meta ]]</b></p> </div> </template> <script> class MyView2 extends MyOptionsMixin(Polymer.Element) { static get is() { return 'my-view2'; } } window.customElements.define(MyView2.is, MyView2); </script> </dom-module> 
my-options.html 我-options.html
 <base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/"> <link rel="import" href="polymer/polymer-element.html"> <link rel="import" href="paper-checkbox/paper-checkbox.html"> <link rel="import" href="iron-meta/iron-meta.html"> <dom-module id="my-options"> <template> <style> :host { display: block; padding: 16px; } h3, p { margin: 8px 0; } </style> <iron-meta key="meta" value="[[options.subscribe]]"></iron-meta> <h3>Options</h3> <p> <paper-checkbox checked="{{ options.subscribe }}">Send Notifications</paper-checkbox> </p> </template> <script> (function() { let optionsInstance = null; class MyOptions extends Polymer.Element { static get is() { return 'my-options'; } static get properties() { return { options: { type: Object, value: () => ({ subscribe: false }) }, subscribers: { type: Array, value: () => [] } } } static get observers() { return [ 'optionsChanged(options.*)' ] } constructor() { super(); if (!optionsInstance) optionsInstance = this; } register(subscriber) { this.subscribers.push(subscriber); subscriber.options = this.options; subscriber.notifyPath('options'); } unregister(subscriber) { var i = this.subscribers.indexOf(subscriber); if (i > -1) this.subscribers.splice(i, 1) } optionsChanged(change) { for(var i = 0; i < this.subscribers.length; i++) { this.subscribers[i].notifyPath(change.path); } } } window.customElements.define(MyOptions.is, MyOptions); MyOptionsMixin = (superClass) => { return class extends superClass { static get properties() { return { options: { type: Object } } } connectedCallback() { super.connectedCallback(); optionsInstance.register(this); } disconnectedCallback() { super.disconnectedCallback(); optionsInstance.unregister(this); } } } }()); </script> </dom-module> 

The short answer is: <iron-meta> doesn't have notification support. 简短的答案是: <iron-meta>没有通知支持。 https://github.com/PolymerElements/iron-meta/issues/9 https://github.com/PolymerElements/iron-meta/issues/9

Your example is relying on the fact the two-way bindings work and your meta property gets updated when the observed key changes. 您的示例依赖于以下事实:双向绑定有效,并且当观察到的键更改时,您的meta属性也会更新。 This is not the case. 不是这种情况。 Internally, values are simply assigned to a global store, and there is not publish/subscribe mechanism in-place. 在内部,仅将值分配给全局存储,并且就位没有发布/订阅机制。 https://github.com/PolymerElements/iron-meta/blob/master/iron-meta.html#L91 https://github.com/PolymerElements/iron-meta/blob/master/iron-meta.html#L91

If you're using Polymer 1.x, maybe this element will satisfy your needs: https://www.webcomponents.org/element/trofrigo/global-variable 如果您使用的是Polymer 1.x,则此元素可能会满足您的需求: https : //www.webcomponents.org/element/trofrigo/global-variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM