2017-10-27 1 views
2

카메라의 가용성에 따라 요소를 표시/숨기려고합니다. 다른 요소의 속성을 기반으로 속성을 계산하려면 어떻게해야합니까?

감지 카메라, 만들-환영 모든 주요 내부에있는 카메라 분리 만들-응용 프로그램을.

<link rel="import" href="../../bower_components/polymer/polymer-element.html"> 
 
<link rel="import" href="../../bower_components/polymer/lib/elements/dom-if.html"> 
 

 
<link rel="import" href="detect-camera.html"> 
 
<link rel="import" href="camera-detached.html"> 
 
<link rel="import" href="create-welcome.html"> 
 

 
<dom-module id="create-app"> 
 
    <template> 
 
     <style> 
 
      :host { 
 
       display: block; 
 
      } 
 
     </style> 
 

 
     <detect-camera found=[[found]]></detect-camera> 
 
     <template is="dom-if" if="{{!allowed}}"> 
 
      <camera-detached></camera-detached> 
 
     </template> 
 
     <template is="dom-if" if="{{allowed}}"> 
 
      <create-welcome></create-welcome> 
 
     </template> 
 
    </template> 
 
    <script> 
 
    class CreateApp extends Polymer.Element { 
 
     static get is() { 
 
     return 'create-app'; 
 
     } 
 

 
     constructor() { 
 
     super(); 
 
     } 
 

 
     static get properties() { 
 
     return { 
 
      allowed: { 
 
      type: Boolean, 
 
      computed: '_isAllowed(found)' 
 
      } 
 
     }; 
 
     } 
 

 
     _isAllowed(found) { 
 
     return found; 
 
     } 
 
    } 
 

 
    window.customElements.define(CreateApp.is, CreateApp); 
 
    </script> 
 
</dom-module>
는 카메라 벗긴 의 속성이 감지 카메라 "을 발견"하면 여부,생성-환영이 논리에 표시됩니다 사실이다 .

카메라를 감지하는 모든 논리는 감지 카메라입니다. 감지 카메라 안의 코드는 속성 "찾음"속성/속성을 설정합니다.

<link rel="import" href="../../bower_components/polymer/polymer-element.html"> 
 

 
<dom-module id="detect-camera"> 
 
    <template> 
 
    </template> 
 

 
    <script> 
 
    class DetectCamera extends Polymer.Element { 
 
     static get is() { 
 
     return 'detect-camera'; 
 
     } 
 

 
     static get properties() { 
 
     return { 
 
      found: { 
 
      type: Boolean, 
 
      value: false, 
 
      reflectToAttribute: true, 
 
      notify: true 
 
      } 
 
     } 
 
     } 
 

 
     constructor() { 
 
     super(); 
 
     this._foundChanged 
 
     } 
 

 
     _foundChanged() { 
 
     this.found = true; 
 
     } 
 
    } 
 

 
    window.customElements.define(DetectCamera.is, DetectCamera); 
 
    </script> 
 
</dom-module>

은 내가 재산 " 발견"계산에 따라 " 허용"계산합니다.

+0

한눈에 'found = [[found]]'따옴표가 누락되었습니다. 또한 100 % 확실하지는 않지만'if {{! allowed}}''에 기대했던대로 작동하지 않을 것이라고 생각합니다. 'this._foundChanged'는 메소드에 대한 유효하지 않은 호출이기도합니다. 마지막으로, 계산 된'isAllowed'를 int로 사용할 수있게하려면'found'에 양방향 바인딩이 필요합니다. – DVM

답변

0

create-app에서 그림자 DOM을 쿼리하고 detect-camera 요소를 찾고 속성을 확인할 수 있습니다.

하지만 그다지 중요하지 않은 Polymer-ish 또는 확장 성이 있습니다. 일반적으로 데이터는 속성/속성에 의해 아래로 흐르고 이벤트에 의해 위로 이동해야합니다.

그래서 나는 당신이 당신은 또한 사용자 정의 이벤트를 트리거 할 속성 found을 설정 detect-camera 요소에서 것을 권장합니다,의는 camera-found을 가정 해 봅시다이 포장 요소에 당신이 그렇지 않은 가정 시작할 것와의 리스너로 업데이트 그 사건.

dispatchEvent에서 here을보고 수신 대기 중입니다. 메모를 잊지 마세요에 대한 bubbles: true, composed: true에서 사용자 정의 이벤트를 수신 할 수 있어야합니다 "외부"

0

를 사용하여 양방향 바인딩 :

을 :

<detect-camera found="{{found}}"></detect-camera> 

또는 found 속성이 변경 수신 당신이 이벤트 리스너 방식을 사용하는 경우

<detect-camera on-found-changed="onFoundChanged"></detect-camera> 

, 당신은 당신의 CreateApp 요소에 로컬 속성을 업데이트해야합니다

onFoundChanged(newVal, oldVal) { 
    this.hasCamera = newVal; 
} 
0

다행스럽게도 "SL"이라는 이름의 "fm"이 코드에서 "누락 된 항목"을 발견했습니다. 그는 코드에 누락 된 인용문과 양방향 바인딩이 있음을 알게되었습니다.

<detect-camera found={{allowed}}></detect-camera>

또한, 우리는 "이 생성-응용 프로그램을 "이 경우에 " 허용"계산 할 필요가 없습니다 <detect-camera found="{{allowed}}"></detect-camera>

해야한다. '찾음'으로 자동 설정되었습니다.

관련 문제