What is the best way to implement declarative collection rendering with Polymer?

混江龙づ霸主 提交于 2019-12-04 17:19:36

If I understand your use case correctly, you want the children of <rendered-collection> to describe the rendering for each item in the collection. This is exactly what <template> is for. So, if we propose usage of <rendered-collection> like so:

<rendered-collection>
  <template>
    <h2>{{name}}</h2>
  </template>
</rendered-collection>

Then we can render it with a bit of template-fu:

<polymer-element name="rendered-collection">
<template>
  <content></content>
</template>
<script>
  Polymer('rendered-collection', {
    collection: [
      {name: 'alpha'},
      {name: 'beta'},
      {name: 'gamma'}
    ],
    ready: function() {
      this.bindTemplate();
    },
    bindTemplate: function() {
      // user-supplied template
      var t = this.querySelector('template');
      // optional, but supplies fancy expression support
      t.bindingDelegate = new PolymerExpressions();
      // repeat over the entire model
      t.setAttribute('repeat', '{{}}');
      // set the model to our collection
      t.model = this.collection;
    }
  });
</script>
</polymer-element>

http://jsbin.com/kedig/1/edit

The code below seems to work but I would like to have feedback on this solution which feels hacky and could be improved:

<rendered-collection url="/api/items">
  <custom-element value="{{ _it_ }}"></custom-element>
</rendered-collection>

with

<polymer-element name="html-render" attributes="html model">
  <template></template>
  <script>
    Polymer({
      htmlChanged: function() {
        var tmpl = document.createElement("template");
        tmpl.setAttribute("bind", "");
        // WARNING: Potential XSS vulnerability
        tmpl.innerHTML = this.html;
        tmpl.model = { _it_: this.model };
        this.shadowRoot.appendChild(tmpl);
        // Needed ?
        Platform.performMicrotaskCheckpoint();
      }
    });
  </script>
</polymer-element>

<polymer-element name="rendered-collection" attributes="url">
  <template>
    <content id="content"></content>
    <core-ajax url="{{ url }}" response="{{ collection }}" auto handleAs="json"></core-ajax>
    <template repeat="{{ item in collection }}">
      <template repeat="{{ node in contentNodes }}">
        <html-render html="{{ node.outerHTML }}" model="{{ item }}"></html-render>
      </template>
    </template>
  </template>
  <script>
    Polymer({
      attached: function () {
        var nodes = this.$.content.getDistributedNodes();
        this.contentNodes = _.isArray(nodes) ? nodes : nodes.array();
        this.$.content.remove();
      }
    });
  </script>
</polymer-element>

Another stab at it using Scott Miles' solution but including <core-ajax>:

<rendered-collection url="/api/items">
  <template>
    <custom-element value="{{ }}"></custom-element>
  </template>
</rendered-collection>

with

<polymer-element name="rendered-collection" attributes="url">
  <template>
    <core-ajax auto
               url="{{ url }}"
               handleAs="json"
               response="{{ collection }}"></core-ajax>
    <content></content>
  </template>
  <script>
    Polymer({
      ready: function() {
        this.clientTemplate = this.querySelector('template');
        this.clientTemplate.bindingDelegate = new PolymerExpressions();
        this.clientTemplate.model = this;
        this.clientTemplate.setAttribute('repeat', '{{ collection }}');
      }
    });
  </script>
</polymer-element>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!