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

Data binding in a dynamically inserted polymer element

有时我们可能需要将自定义元素动态添加到上下文中。那么:

我已经为 0.5 和 1.0 创建了一个示例。请参阅下面进行注射的聚合物的代码。为了清楚起见,您还可以查看完整的 plunker 示例。

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>

请查看完整的 plunkr 示例:http://plnkr.co/edit/2Aj3LcGP1t42xo1eq5V6?p=preview

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>

请查看完整的 plunkr 示例:http://plnkr.co/edit/K463dqEqduNH10AqSzhp?p=preview

您知道 Polymer 1.0 的一些解决方法或等效方法吗?

不幸的是,我认为不可能通过 "clean" 方式来做到这一点。要替换 Path Observer,我们必须在 "foo" 值更改上添加 link 到动态元素。第一步是观察 "foo" 属性 值的变化。第二步是将更改复制到创建的每个动态元素。

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

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

目前还没有直接的方法。您应该通过监听父元素的 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>

您可以在此处查看工作示例:http://plnkr.co/edit/mZd7BNTvXlqJdJ5xSq0o?p=preview