2017-02-16 3 views
1

하나의 Polymer 구성 요소에 둘 이상의 템플릿이 있고 속성을 기반으로 특정 템플릿으로 해석하는 방법을 알아 내려고 여기에서 약간 회색으로 표시하려고합니다. 바의 속성을 가지고 <py-test></py-test> 경우Polymer : 속성을 기반으로 한 동일한 구성 요소에 대한 여러 템플릿?

구성 요소

<dom-module id="py-test"> 
    <template> 
     Foo 
    </template> 
    <template> 
     Bar 
    </template> 
    <script> 
     Polymer({ 
      is: "py-test" 
     }) 
    </script> 
</dom-module> 

마크 업

<py-test bar></py-test> 

예상 결과 : 여기

는 시나리오이다 구성 요소는 두 번째 템플릿을 사용해야합니다. 그렇지 않으면 첫 번째 템플릿을 사용해야합니다.

누구든지 Polymer를 제출하는 방법에 대한 단서가 있습니까? // 편집

마리아

지적 DOM-경우 가능한 솔루션으로. 어떤 일을하는지. 나는 DOM-경우 검사 추가 위의 예제를 사용 :

<dom-module id="py-test"> 
    <template> 
     <template is="dom-if" if="[[foo]]"> 
      Foo 
     </template> 
     <template is="dom-if" if="[[bar]]"> 
      Bar 
     </template> 
    </template> 
    <script> 
     Polymer({ 
      is: "py-test", 
      properties: { 
       foo: { 
        type: Boolean, 
        value: true 
       }, 
       bar: { 
        type: Boolean, 
        value: false 
       } 
      } 
     }) 
    </script> 
</dom-module> 

을하지만 난이 <py-test> 요소 자체에 설정된 속성으로이 넥타이 수있는 방법을 볼 수 없습니다.

+1

'dom-if'를 옵션으로 사용 하시겠습니까? – Maria

+0

@Maria 특정 기능의 진실성을 확인함으로써 템플리트간에 전환 할 수 있다는 점에서 dom-if와는 종류가 다릅니다. 하지만이 방법은 부울 결과를 마크 업 요소에 설정된 특정 특성에 연결하는 방법을 알 수 없기 때문에 많은 도움이되지 못합니다. 어떤 아이디어? –

+0

어쩌면 이것이 [iron-lazy-pages] (https://github.com/TimvdLippe/iron-lazy-pages)를 찾고있는 것일 수 있습니다. Polymer.Templatizer 비헤이비어를 사용하여 속성을 기반으로 템플리트를 스탬프합니다. –

답변

2

거의 편집 했으니 까. properties 개체의 초기 값을 제거하기 만하면됩니다.

<dom-module id="py-test"> 
    <template> 
     <template is="dom-if" if="[[foo]]"> 
      Foo 
     </template> 
     <template is="dom-if" if="[[bar]]"> 
      Bar 
     </template> 
    </template> 
    <script> 
     Polymer({ 
      is: "py-test", 
      properties: { 
       foo: { 
        type: Boolean, 
       }, 
       bar: { 
        type: Boolean, 
       } 
      } 
     }) 
    </script> 
</dom-module> 

당신은 다음과 같이 사용할 수 있습니다 :

<py-test foo></py-test> 
<py-test bar></py-test> 

가 여기 plunk입니다.

Polymer에서 부울 속성을 처리하는 방법이 다소 혼란 스러울 수 있습니다. 존재하는 경우 관련된 속성은 true이고, 그렇지 않으면 false입니다. 초기 값을 설정하면 방해가됩니다.

+0

우수 설명. 사실, Polymer는 그 단점을 가지고 있지만, 대부분의 프레임 워크는 어쨌든합니다. 고맙습니다! –

관련 문제