简体   繁体   English

聚合物嵌套dom-repeat

[英]Polymer nested dom-repeat

I'm having some trouble with Polymer's <dom-repeat> . 我在使用Polymer的<dom-repeat>遇到了一些麻烦。

I have parent objects (folders) and child objects (content of the folders). 我有父对象(文件夹)和子对象(文件夹的内容)。 Both are computed from responses to an AJAX request. 两者都是根据对AJAX请求的响应计算出来的。

The result should be something like: 结果应该是这样的:

Folder

  • child 儿童
  • child 儿童
  • child 儿童

My code: 我的代码:

<iron-ajax id="folder" url="../Folder" auto last-response="{{folders}}"></iron-ajax>

<template is="dom-repeat" items="{{folders.items}}" as="folder">
  <paper-card  on-tap="openFolder" object item="[[folder]]">

    <custom-object value="[[folder]]" link></custom-object>

    <iron-ajax id="folder" url="..[[folder.id]]/children" auto last-response="{{children}}"></iron-ajax>

    <template is="dom-repeat" items="{{children.items}}" as="children"> 
      <custom-object style="margin-top: 15px" value="[[children]]" link></custom-object>
    </template>

  </paper-card>
</template>

The issue is every folder becomes the child of the last folder and not his own. 问题是每个文件夹都成为最后一个文件夹的子项而不是他自己的子文件夹。

Your inner iron-ajax writes to the same children property in each iteration of the repeater. 你的内部iron-ajax在转发器的每次迭代中写入相同的children属性。 The repeater does not scope that property, and it's actually visible to each iteration and to the entire template outside the repeater. 转发器不限制该属性,并且它实际上对每个迭代和转发器外部的整个模板可见。 It's likely that subsequent iterations are overwriting the children of the previous iteration, which manifests as unexpected nesting of folders. 这可能是因为后续的迭代覆盖children上一次迭代,这体现了文件夹的意料之外嵌套。

One way to address this is to scope the children property to each iteration by attaching it to the iterator (ie, folder in this case). 解决此问题的一种方法是通过将children属性附加到迭代器(即本例中的folder )来将children属性的范围限定为每次迭代。 To avoid a potential name collision with an actual property, it would be a good idea to prefix it (eg, folder._children ), as seen in this example: 为了避免与实际属性发生潜在的名称冲突,最好在它folder._children它(例如, folder._children ),如下例所示:

<iron-ajax id="folder" url="../Folder" auto last-response="{{folders}}"></iron-ajax>

<template is="dom-repeat" items="{{folders.items}}" as="folder">
  <paper-card  on-tap="openFolder" object item="[[folder]]">

    <custom-object value="[[folder]]" link></custom-object>


    <!--
        attach a new property `_children` to `folder` to contain the response for this folder
    -->
    <iron-ajax url="..[[folder.id]]/children" auto last-response="{{folder._children}}"></iron-ajax>


    <template is="dom-repeat" items="{{folder._children.items}}" as="children"> 
      <custom-object style="margin-top: 15px" value="[[children]]" link></custom-object>
    </template>
  </paper-card>
</template>

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

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