简体   繁体   English

动态插入的聚合物元件中的数据绑定

[英]Data binding in a dynamically inserted polymer element

There're some times when we could need adding a custom element dynamically into a context. 有时候我们需要动态地将自定义元素添加到上下文中。 Then: 然后:

  • The inserted polymer could receive some properties bound to another property inside the context, so it can change accordingly. 插入的聚合物可以接收绑定到上下文中的另一个属性的一些属性,因此它可以相应地改变。

  • At polymer 0.5 we could use PathObserver to binding a property to a context property for a recently added component. 在聚合物0.5处,我们可以使用PathObserver将属性绑定到最近添加的组件的上下文属性。 However, I did not find a workaround or equivalent at polymer 1.0. 但是,我没有找到聚合物1.0的解决方法或等效物。

I have created an example for 0.5 and just the same for 1.0. 我创建了一个0.5的示例,并为1.0创建了相同的示例。 See below the code of the polymer that it makes the injection. 请参阅下面的注射聚合物代码。 Also you can see the full plunker examples for clarity. 此外,您还可以看到完整的plunker示例,以便清晰。

Ej 0.5: Ej 0.5:

<polymer-element name="main-context">
  <template>
    <one-element foo="{{foo}}"></one-element>
    <div id="dynamic">
    </div>
  </template>
  <script>
    Polymer({
      domReady: function() {
        // injecting component into polymer and binding foo via PathObserver
        var el = document.createElement("another-element");
        el.bind("foo", new PathObserver(this,"foo"));
        this.$.dynamic.appendChild(el);
      }
    });
  </script>
</polymer-element>

Please, see the full plunkr example: http://plnkr.co/edit/2Aj3LcGP1t42xo1eq5V6?p=preview 请参阅完整的plunkr示例: http ://plnkr.co/edit/2Aj3LcGP1t42xo1eq5V6?p = preview

Ej 1.0: Ej 1.0:

<dom-module id="main-context">
  <template>
    <one-element foo="{{foo}}"></one-element>
    <div id="dynamic">
    </div>
  </template>
</dom-module>
<script>
  Polymer({
    is: "main-context",
    ready: function() {
      // injecting component into polymer and binding foo via PathObserver
      var el = document.createElement("another-element");
      // FIXME, there's no a path observer: el.bind("foo", new PathObserver(this,"foo"));
      this.$.dynamic.appendChild(el);
    }
  });
</script>

Please, see the full plunkr example: http://plnkr.co/edit/K463dqEqduNH10AqSzhp?p=preview 请参阅完整的plunkr示例: http ://plnkr.co/edit/K463dqEqduNH10AqSzhp?p = preview

Do you know some workaround or equivalent with polymer 1.0? 你知道聚合物1.0的一些变通方法或等效方法吗?

Right now, there is no direct way to do it. 现在,没有直接的方法来做到这一点。 You should manually do the double binding by listening to changes in the foo property of the parent element and listening to the foo-changed event of the programmatically created element. 您应该通过侦听父元素的foo属性中的更改并侦听以编程方式创建的元素的foo-changed事件来手动执行双重绑定。

<script>   
Polymer({
  is: "main-context",
  properties: {
    foo: {
      type: String,
      observer: 'fooChanged'
    }
  },
  ready: function() {
    var self = this;
    var el = this.$.el = document.createElement("another-element");

    //set the initial value of child's foo property
    el.foo = this.foo;
    //listen to foo-changed event to sync with parent's foo property
    el.addEventListener("foo-changed", function(ev){
      self.foo = this.foo;
    });

    this.$.dynamic.appendChild(el);
  },
  //sync changes in parent's foo property with child's foo property
  fooChanged: function(newValue) {
    if (this.$.el) {
      this.$.el.foo = newValue;
    }
  }
});
</script>

You can see a working example here: http://plnkr.co/edit/mZd7BNTvXlqJdJ5xSq0o?p=preview 你可以在这里看到一个有效的例子: http//plnkr.co/edit/mZd7BNTvXlqJdJ5xSq0o?p = preview

Unfortunately I think it's not possible to do this by a "clean" way. 不幸的是,我认为不可能以“干净”的方式做到这一点。 To replace the Path Observer, we have to add link on the "foo" value changes to the dynamic elements. 要替换Path Observer,我们必须在动态元素的“foo”值上添加链接。 The first step is observe the "foo" property value changes. 第一步是观察“foo”属性值的变化。 The second step is replicate the changes to each dynamic elements created. 第二步是复制对创建的每个动态元素的更改。

<dom-module id="main-context">
  <template>
    <one-element foo="{{foo}}"></one-element>
    <div id="dynamic">
    </div>
  </template>
</dom-module>
<script>
Polymer({
  is: "main-context",
  // Properties used to make the link between the foo property and the dynamic elements.
  properties: {
    foo: {
      type: String,
      observer: 'fooChanged'
    },
    dynamicElements: {
      type: Array,
      value: []
    }
  },
  ready: function() {
    // injecting component into polymer and binding foo via PathObserver
    var el = document.createElement("another-element");
    // Keeps a reference to the elements dynamically created
    this.dynamicElements.push(el);
    this.$.dynamic.appendChild(el);
  },
  // Propagates the "foo" property value changes
  fooChanged: function(newValue) {
    this.dynamicElements.forEach(function(el) {
      el.foo = newValue;
    });
  }
});
</script>

See the full Plunkr example: http://plnkr.co/edit/TSqcikNH5bpPk9AQufez 查看完整的Plunkr示例: http ://plnkr.co/edit/TSqcikNH5bpPk9AQufez

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

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