2014-08-29 3 views
3

나는 div 안에 모든 어린이들을 특정 클래스로 묶어야하는 컨테이너 인 form-el입니다. 각 양식 요소에서이 div을 반복하는 대신에 from-el을 감싸는 것을 허용하고 싶습니다. 모든 요소를 ​​반복하여 다른 HTML 태그 안에 감쌀 수 있습니까? 이를 달성하기장식 요소 <content>

// Markup 
<form-el> 
    <input-el label="name" type="text" /> 
    <span>This must also be wrapped</span> 
</form-el> 

// Would produce 
<form> 
    <div class="form-field"> 
     <label>name</label> 
     <input type="text" name="name" /> 
    </div> 
    <div class="form-field"> 
     <span>This must also be wrapped</span> 
    </div> 
</form> 
// Where '<div class="form-field">' is produced by 'from-el' 

답변

5

한 가지 방법과 같이, 너희 빛을-DOM에 맞는 <content> 노드를 템플릿 - 반복하는 것입니다 :

<polymer-element name="form-el"> 
<template> 

    <template repeat="{{wrappers}}"> 
    <div style="border: 2px solid orange; padding: 2px; margin: 4px"> 
     <content select="[key='{{}}']"> 
    </div> 
    </template> 

</template> 
<script> 

    Polymer({ 
    domReady: function() { 
     this.updateWrappers(); 
    }, 
    updateWrappers: function() { 
     this.wrappers = []; 
     for (var i=0, n=this.firstChild; n; n=n.nextSibling) { 
     if (n.setAttribute) { 
      // attach a selectable key to each light-dom node 
      n.setAttribute('key', i); 
      // specify a <content> with that key 
      this.wrappers.push(i++); 
     } 
     } 
    } 
    }); 

</script> 
</polymer-element> 

http://jsbin.com/mukip/5/edit

+0

고마워요. 하지만 폴리머 요소를 래핑하려고하면 첫 번째 요소 만 표시됩니다. http://jsbin.com/sadexakoyuco/1/edit –

+0

사용자 정의 요소는 무효가 될 수 없습니다. ''을 사용할 수 없으며 ''을 사용해야합니다. –

+0

@ScottMiles 어떻게하면 래퍼를 얻을 수 있습니까? 템플릿이 스탬프 처리되어 비동기 적으로 배포되기 때문에'this.updateWrappers();가 실행 된 직후'this.shadowRoot.querySelectorAll ("div")'에 대한 참조를 얻을 수 없다. 단순화 된 경우 http://jsbin.com/zuxiqo/1/edit에서 작동하지만 경쟁 조건을 피하는 것으로 충분합니까? – tomalec