聚合物1.0:从铁选择器(使用模板重复)调用来自iron-ajax的数据,只获得一个以上的属性

时间:2015-08-19 03:20:51

标签: ajax selector repeat polymer-1.0 iron

使用此代码,我可以打印“主要内容”区域中所选项目的 {{item.name}} (即标签模板之外) is =“dom-repeat”)使用变量{{selected}} attr-for-selected =“name”但我想从所选项目打印更多详细信息,而不仅仅是一个属性,在这种情况下是名称。

          <!-- Main Contetnt -->
          <div class="content">
          <h1>{{selected}}</h1>   *This one works fine*

          <h1>{{item.city}}</h1>   *But these two don't*
          <h1>{{item.date}}</h1>
          </div>

我想打印城市日期以及打印的名称  

{{选择}}

任何人都有这个问题的线索吗? PS:我的元素在代码的底部调用自己,它现在正在工作。但是我可以因此而遇到任何问题吗?

  <dom-module id="x-app">
      <template id="app">

    <iron-ajax url="data/contacts.json" last-response="{{data}}" auto></iron-ajax>

    <paper-drawer-panel id="navDrawerPanel" responsive-width="1280px">

      <div class="nav" drawer>
        <!-- Nav Content -->
      </div>

      <paper-drawer-panel id="mainDrawerPanel" class="main-drawer-panel" main responsive-width="600px"
          drawer-width="[[_computeListWidth(_isMobile)]]" drawer-toggle-attribute="list-toggle"
          narrow="{{_isMobile}}">

        <paper-header-panel class="list-panel" drawer>

          <!-- List Toolbar -->
          <paper-toolbar>
            <paper-icon-button icon="menu" paper-drawer-toggle></paper-icon-button>
          </paper-toolbar>

          <iron-selector selected="{{selected}}" attr-for-selected="name" id="selector">

            <template is="dom-repeat" class="list" items="{{data}}" as="item" id="tmpl">

          <div name="{{item.name}}" on-click="_listTap">
            <div class="item">
              <img class="avatar" src="{{item.image}}">
              <div class="pad">
                <div class="primary">
                  <span>{{index}}</span>
                  <span>{{item.name}}</span>
                </div>
                <div class="secondary">{{item.date}}</div>
                <div class="secondary dim">{{item.city}}</div>
              </div>
              <!-- <iron-icon icon$="{{iconForItem(item)}}"></iron-icon> -->
            </div>
            <div class="border"></div>
          </div>

        </template>

        </iron-selector>


        </paper-header-panel>

        <paper-header-panel class="content-panel" main>

          <!-- Main Toolbar -->
          <paper-toolbar>
            <paper-icon-button icon="arrow-back" list-toggle></paper-icon-button>
          </paper-toolbar>

            <!-- Main Contetnt -->
          <div class="content">
          <h1>{{selected}}</h1> 
          <h1>{{item.city}}</h1>
          <h1>{{item.date}}</h1>
          </div>


        </paper-header-panel>

      </paper-drawer-panel>

    </paper-drawer-panel>

  </template>

  <script>

    Polymer({

      is: 'x-app',
          properties: {

          },
        ready: function() {
         var drawerPanel = document.querySelector('#mainDrawerPanel');
         drawerPanel.openDrawer();

          },

      _computeListWidth: function(isMobile) {
        // when in mobile screen size, make the list be 100% width to cover the whole screen
        return isMobile ? '100%' : '33%';
      },

      _listTap: function() {
        var drawerPanel = document.querySelector('#mainDrawerPanel');
         drawerPanel.closeDrawer();
     }

    });

  </script> 


</dom-module>

<body>
    <x-app></x-app>
</body>

1 个答案:

答案 0 :(得分:1)

使用selected-item代替selected

selectedItem会返回当前所选的项目(请参阅IronSelectableBehavior

所以你的iron-selector应该是这样的:

<iron-selector selected-item="{{selectedItem}}" attr-for-selected="name" id="selector">

然后向div添加两个条件属性,如下所示:

<div name="{{item.name}}" city$="[[item.city]]" date$="[[item.date]]" on-click="_listTap">

然后设置一个观察者来监控对selectedItem

的更改
...

observers: [ '_selectedItemChanged(selectedItem)' ],

_selectedItemChanged: function(el) {
  this.city = el.getAttribute('city');
  this.date = el.getAttribute('date');
},

...

然后您可以使用以下内容:

<div class="content">
  <h1>{{city}}</h1>
  <h1>{{date}}</h1>
</div>