Polymer 聚合物Dom重复绑定到内容

Polymer 聚合物Dom重复绑定到内容,polymer,polymer-1.0,Polymer,Polymer 1.0,在polymer 1.2.3中,dom repeat是否可以将内容用作模板并将值绑定到提供的元素 自定义元素: <dom-module id="modify-collection"> <template> <div> <template is="dom-repeat" items="[[collection]]"> <content></content> </temp

在polymer 1.2.3中,
dom repeat
是否可以将内容用作模板并将值绑定到提供的元素

自定义元素:

<dom-module id="modify-collection">
  <template>
    <div>
      <template is="dom-repeat" items="[[collection]]">
        <content></content>
      </template>
    </div>
  </template>
</dom-module>

用法:

<modify-collection collection="{{things}}">
  <list-item resource="[[item]]"></list-item>
</modify-collection>

我在没有帮助的情况下查看了以下资源:



更新2016-02-03:Polymer团队计划在未来对此提供更好的支持,以帮助解决一些缺点。

以铁名单元素为例。这里,它抓取传递的模板元素。 templatizer mixin用于实现对传递的模板的绑定(它也被
使用)

TLDR 解决方案2虽然有点冗长,但似乎效果最好。它还允许您根据模型的属性动态选择模板。(参见解决方案2:高级示例)


背景故事 在这个简单的示例中发生了很多事情,包括模板化、“获取”shadow?、shady和light DOM之间的内容,当然还有数据绑定

这个问题提供了一些见解,但如果没有
Polymer 0.5的HTML
,它实际上是不可行的()。问题在于,当我们试图使用innerHTML将元素复制到模板时,数据绑定似乎丢失了(AFAIK)

因此,如果没有这些,我们就无法使用数据绑定动态创建模板。因此,这两种解决方案都提前将内容包装在模板中;这导致html是惰性的,并允许在适当的时间绑定数据()

如果您真的想了解所有内容,我建议您阅读Polymer
src
for
lib/template/dom repeat.html
lib/template/templatizer.html
lib/annotations/annotations.html
(~1500行)


解决方案1- 有关改进的解决方案1,请参阅

注意,这种方法会导致
dom repeat
无法自动渲染内容,因此我们称之为手动渲染

元素 元素

用法

高级元素 用法不会从上面改变

这里我们要做的是引入一个间接级别,允许我们包装传递给元素的模板(在我们的示例中,使用覆盖)


...
特性:{
_覆盖模式:{
类型:布尔型,
值:false
}
},
_templateRef:函数(覆盖模式){
返回覆盖模式?'wrapper':'temp';
}

FWIW,它看起来与解决方案1一样有效,并向其中添加了通知路径(以及自动绑定/更改等):


聚合物({
是‘我——我’,
特性:{
收藏:{
类型:数组,
价值:[
{id:1,名称:'a'},
{id:2,名称:'b'}
],
通知:正确
}
},
就绪(){
this.$.repeater.templatize(this.querySelector('#templ');
Polymer.Bind.prepareModel(这个$.repeater);
Polymer.Base.prepareModelNotifyPath(此.$.repeater);
}
});
那么就用这个:

<i-me>
  <template id="templ">
  <p><span>[[item.id]]</span>: <span>[[item.name]]</span></p>
  </template>
</i-me>

[[item.id]]:[[item.name]]


@btelles,请看下面我的mémoire。我想知道是否有聚合物2.x或3.x解决方案。感谢Gunter的建议,尽管由于问题的复杂性,我选择写一个更详细的答案。这是更干净的解决方案。谢谢是否有任何方法可以设置要从外部重复的内容的样式(即从
的父元素)?我正在试图弄清楚,在多个级别中,最顶层定义了内部级别,但第二个级别的嵌套正在击败我:
<modify-collection collection="{{things}}">
  <template id="templ"><list-item resource="[[item]]"></list-item></template>
</modify-collection>
!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
  Portions of this code have been adapted from the `grapp-template-ref` element.

  The original copyright notices are below.
-->
<!--
MIT License
Copyright (c) 2014-2015 Dirk Grappendorf, www.grappendorf.net

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 -->

<!--

 The `dom-ref` element is a custom `HTMLTemplateElement` type extension that
can be used to reference another template by id using the `ref` property.
`dom-bindref` accepts a `bind` property to bind an object to the referenced
template. By default the bound object can be accessed as `item`, this can be
changed using the `as` property.

Example:

```html
<template is="dom-template" id="template-bind"><span>[[item.key]]</span></template>
<template is="dom-ref" ref="template-bind" bind='{"key":"value"}'></dom-ref>
```

-->
<!-- <link rel="import" href="templatizer.html"> -->

<script>

  Polymer({

    is: 'dom-ref',
    extends: 'template',

    /**
     * Fired whenever DOM is added or removed by this template (by
     * default, rendering occurs lazily).  To force immediate rendering, call
     * `render`.
     *
     * @event dom-change
     */

    properties: {

      /**
       * Reference to another template's id.
       */
      ref: {
        type: String,
        observer: '_refChanged'
      },

      /**
       * Object to be bound to referenced template.
       */
      bind: {
        type: Object,
        observer: '_bindChanged'
      },

      /**
       * The name of the variable to add to the binding scope for the
       * element associated with a given template instance.
       */
      as: {
        type: String,
        value: 'item'
      }
    },

    behaviors: [
      Polymer.Templatizer
    ],

    ready: function() {
      this.templatize(this);
    },

    attached: function() {
      return this._stamp();
    },

    detached: function() {
      return this._removeChildren();
    },

    _refChanged: function(newRef, oldRef) {
      if (oldRef) {
        this._removeChildren();
        return this._stamp();
      }
    },

    _bindChanged: function(newBind, oldBind) {
      if (oldBind) {
        this._removeChildren();
        return this._stamp();
      }
    },

    _stamp: function() {
      var root, template, templateRoot, bind = {};
      this._parent = Polymer.dom(this).parentNode;
      root = this._parent;

      while (Polymer.dom(root).parentNode) {
        root = Polymer.dom(root).parentNode;
      }
      template = Polymer.dom(root).querySelector("template#" + this.ref);

      // Check For Light Dom Elements that may be passed to this shadow root (Useful for: `<content></content>`)
      if (!template) {
        template = root.host.querySelector("template#" + this.ref);
      }

      // Check the whole document
      if (!template) {
        template = document.querySelector("template#" + this.ref);
      }
      bind[this.as] = this.bind;

      // templateRoot = template.stamp(bind).root;
      // Use this method until this lands: https://github.com/Polymer/polymer/pull/1889
      templateRoot = (new template.ctor(bind, template)).root;
      this._children = Array.prototype.slice.call(templateRoot.childNodes);
      return Polymer.dom(this._parent).insertBefore(templateRoot, this);
    },

    _removeChildren: function() {
      var child, i, len, ref, results;
      if (this._children) {
        ref = this._children;
        results = [];
        for (i = 0, len = ref.length; i < len; i++) {
          child = ref[i];
          results.push(Polymer.dom(this._parent).removeChild(child));
        }
        return results;
      }
    }

  });

</script>
<dom-module id="modify-collection">
  <template>
    <div>
      <content></content>
      <template is="dom-repeat" items="[[collection]]">
          <template is="dom-ref" bind="[[item]]" ref="templ"></template>
      </template>
    </div>
  </template>
</dom-module>
<modify-collection collection="{{things}}">
  <template id="templ" is="dom-template"><list-item resource="[[item]]"></list-item></template>
</modify-collection>
<dom-module id="modify-collection">
  <template>
    <div>
      <content></content>
      <template id="wrapper" is="dom-template">
        <div class="overlay">
          <template is="dom-ref" bind="[[item]]" ref="templ"></template>
        </div>
      </template>

      <template is="dom-repeat" items="[[collection]]">
        <template is="dom-ref" bind="[[item]]" ref="[[_templateRef(_overlayMode)]]"></template>
      </template>
    </div>
  </template>

  <script>
    ...
    properties: {
      _overlayMode: {
        type: Boolean,
        value: false
      }
    },
    _templateRef: function(overlayMode) {
      return overlayMode ? 'wrapper' : 'templ';
    }
  </script>
</dom-module>
<dom-module id="i-me">
  <template>
    <content></content>
    <template is="dom-repeat" id="repeater" items="[[collection]]"></template>
  </template>
  <script>
    Polymer({
      is: 'i-me',
      properties: {
        collection: {
          type: Array,
          value: [
            {id: 1, name: 'a'},
            {id: 2, name: 'b'}
          ],
          notify: true
        }
      },
      ready() {
        this.$.repeater.templatize(this.querySelector('#templ'));
        Polymer.Bind.prepareModel(this.$.repeater);
        Polymer.Base.prepareModelNotifyPath(this.$.repeater);
      }
    });
  </script>
</dom-module>
<i-me>
  <template id="templ">
  <p><span>[[item.id]]</span>: <span>[[item.name]]</span></p>
  </template>
</i-me>