2017-02-05 5 views
0

子要素からその親にカスタムイベントを送出しようとしていますが、リスナーオブジェクトを使用していません。注釈付きイベントを使用して動作しますが、ほとんどの状況では適用されません。この場合、どのようにリスナーオブジェクトを使用できますか。あなたの助けを事前に親要素でキャッチされていない子要素によって発生したポリマーカスタムイベント

<!--in child element's dom..--> 

<dom-module id="searchresult-controller"> 
    <template> 
     <style> 
      ... 
     </style> 
    </template> 
    <script type="text/javascript"> 
     Polymer({ 
      is: "searchresult-controller", 

       ready: function(){ 
        this.sendSignal(); 
      }, 

      sendSignal: function() { 
       this.fire('msg', {data: 'i love polymer'}); 
      } 
     }); 
    </script> 
</dom-module> 

<!-- in parent --> 
<dom-module id="search-controllers"> 
<template> 
    <style> 
     ... 
    </style> 
    <searchresult-controller></searchresult-controller> 
    <result-view></result-view> 
</template> 

<script type="text/javascript"> 
    Polymer({ 
     is: "search-controllers", 

     listeners: { 
      'msg': '_showAlert' 
     }, 
     _showAlert: function(e) { 
      console.log(e.detail.data); 
     } 

    }); 
</script> 

ありがとう:ここでの問題を説明するためにいくつかのコードです。

答えて

0

あなたはattachedなくreadysearchresult-controllerからイベントを発射しようとしてもらえますか?

readysearch-controllersaccording to the docs、以来、listenersオブジェクトに設定されたリスナーがattachedライフサイクルイベント中に設定されている処理するためにもすぐのためかもしれません。

私の推測ではsearchresult-controllerreadyのイベントは、search-controllersattachedイベントの前に発生します。

+0

d docsへの参照に感謝します。それでも、イベントに関しては、main.jsから鉄信号イベントを発射しようとしました。 var event = new CustomEvent( 'iron-signal'、{'detail':{name: 'action'、data: "foo" }})); document.dispatchEvent(event); 他の要素は鉄信号を使用して聴きましたが、結果は聞こえませんでした。どのように私はこれを行うことができますし、高分子のおかげでそこに良い本があることをお勧めすることができます私はポリマーのおかげでアプリケーションを構築するための基礎を理解して強力な基礎を構築する必要があります。 – jobie

+1

あなたは ''をどのように設定しましたか?具体的には、 'action'イベントをどのように聴こうとしましたか?書籍についてはわかりませんが、[GitHubのポリマーコンポーネントのソースコード](https://github.com/PolymerElements)を読むと多くのことが分かります。 –

関連する問題