2016-02-05 75 views
0

我正在使用Polymer 1.0,並且正在嘗試從以下模板中的腳本訪問items在聚合物dom-repeat中定位元素

我花了一段時間,我只是無法從這個模板的範圍中弄清楚如何訪問'my-swiper-icon'元素。

總之,如何找到「#slide_n #item_m」?該路徑不適用於查詢選擇器。爲了完整性#slide_n可以工作,但它有一個#document_fragment子元素,所以它沒有用......對吧?

<dom-module id="my-swiper"> 
    <template> 
    <style include="my-swiper-import"> 
     :host { 
     display: block; 
     } 
    </style> 

    <!-- Swiper --> 
    <div class="swiper-container"> 
     <div class="swiper-wrapper"> 
      <template id="slides_template" is="dom-repeat" items="{{slides}}"> 
      <div class="swiper-slide" id="slide_{{index}}"> 
       <div> 
       <template id="items_template" is="dom-repeat" items="{{item.items}}"> 
        <my-swiper-icon id="item_{{index}}" data="{{item}}"/> 
       </template> 
       <div> 
      </div> 
      </template> 
     </div> 
     <!-- Add Pagination --> 
     <div class="swiper-pagination"></div> 
     <!-- Add Arrows --> 
     <div class="swiper-button-next"></div> 
     <div class="swiper-button-prev"></div> 
    </div> 

    </template> 
    <script> 
    (function() { 
     'use strict'; 

     Polymer({ 
     is: "my-swiper", 

     properties: { 
      slides: { 
      type: Array, 
      value: [], 
      notify: true, 
      observer: '_slidesChanged' 
      } 
     }, 

     listeners: { 
      'tap': '__tap', 
      'resize': '__resized' 
     }, 

     __tap: function(e) {  
      // 
     }, 

     __resized: function(e) { 
      // How do I locate "#slide_n #item_m" ???? 
     }, 

     _slidesChanged: function(newValue, oldValue) { 
      // 
     }, 

     ready: function() { 
      this.slides = [     
      { 
       items: [ 
       { hash: 'foobar' }, 
       { hash: 'foobar' }, 
       { hash: 'foobar' }, 
       { hash: 'foobar' } 
       ] 
      }, 
      { 
       items: [ 
       { hash: 'barfoo' }, 
       { hash: 'barfoo' } 
       ] 
      } 
      ]; 
     }, 

     attached: function() { 
      this.async(function() { 
      var swiper = new Swiper('.swiper-container', { 
       pagination: '.swiper-pagination', 
       paginationClickable: '.swiper-pagination', 
       nextButton: '.swiper-button-next', 
       prevButton: '.swiper-button-prev', 
       spaceBetween: 30, 
       hashnav: true 
      }); 
      this.__resized(null); 
      window.addEventListener("resize", this.__resized.bind(this)); 
      }); 
     } 

     }); 

    })(); 
    </script> 

</dom-module> 
+0

你是對的,謝謝@GünterZöchbauer –

回答

1

聚合物提供this$.someId速記訪問一個元素的本地DOM元素,但這並不用於動態創建的元素內template="dom-if"template="dom-repeat"appendNode()工作(例如,... 對於動態創建的節點用Polymer.dom(this.root).querySelectorAll(selector)代替