首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >聚合物嵌套多态重复

聚合物嵌套多态重复
EN

Stack Overflow用户
提问于 2016-09-23 14:18:44
回答 1查看 1.4K关注 0票数 4

我对聚合物的<dom-repeat>有一些麻烦。

我有父对象(文件夹)和子对象(文件夹的内容)。这两种方法都是根据对AJAX请求的响应计算的。

其结果应该是:

文件夹

  • 孩子
  • 孩子
  • 孩子

我的代码:

代码语言:javascript
复制
<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>

问题是每个文件夹都成为最后一个文件夹的子文件夹,而不是自己的文件夹。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-24 04:51:21

在中继器的每次迭代中,您的内部iron-ajax都会写入相同的children属性。中继器不对该属性进行范围设置,它实际上对每个迭代和中继器外的整个模板都是可见的。很可能后续的迭代正在覆盖上一次迭代的children,这显示为文件夹的意外嵌套。

解决这一问题的一种方法是将children属性附加到迭代器(在本例中为folder )。为了避免名称与实际属性发生冲突,最好在其前缀(例如folder._children)上加上前缀,如本例所示:

代码语言:javascript
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39663087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档