2015-06-12 60 views
1

我一直在嘗試創建一個事件來觸發給定某些條件(更一般的情況下),但我還沒有找到一種方法,至少不是在官方文檔中聚合物處理事件創建聚合物自定義元素的自定義事件

https://www.polymer-project.org/0.5/articles/communication.html

的自定義元素聽另一個事件,所以他需要一個條件得到滿足扔內部事件。

這是一個小的描述例如:

的Index.html

... 
<my-element></my-element> 
... 
<script> 
    var ele = document.querySelector("my-element"); 
    ele.addEventListener("myCustomEvent", 
       function(){ 
        // some action 
        ... 
      }) 
</script> 

這個代碼描述誰將會聽取事件( 「myCustomEvent」)和動作被引爆。


另一方面而來的自定義元素的實施

<polymer-element name="my-element"> 
    <template> 
     <other-custom-element id="foo"></other-custom-element> 
    </template> 
    <script> 
    Polymer("my-element", { 
     ready: function(){ 
     var foo = this.$.foo; 
     foo.addEventListener("AnotherEvent" , function(){ 
      ... 
      if(condition){ 
       ... 
       // in this part I need to fire the event 
       // "myCustomEvent" 
      } 
     }) 
     } 
    }) 
    </script> 
</polymer-element> 

我的問題是觸發事件時的條件已經完了,外面聽該事件(在的index.html

參考

  1. https://www.polymer-project.org/0.5/articles/communication.html
  2. https://www.polymer-project.org/0.5/docs/polymer/polymer.html#fire

回答

7

試試這個。 (注意:更改的事件名稱不區分大小寫['AnotherEvent' - >'another-event'],因爲HTML更快樂):

<polymer-element name="my-element"> 
    <template> 
     <other-custom-element on-another-event="{{doAnotherEvent}}"></other-custom-element> 
    </template> 
    <script> 
    Polymer("my-element", { 
     doAnotherEvent: function(){ 
     if (condition){ 
      // in this part I need to fire the event 
      // "my-custom-event" 
      this.fire('my-custom-event'); 
     } 
     } 
    }); 
    </script> 
</polymer-element> 
+0

thanks!這解決了我的問題! – ljofre

+1

對於聚合物1.0存在一些不同的方式? – ljofre

+2

在1.0中:使用'dom-module'而不是'polymer-element',使用'on-another-event =「doAnotherEvent」'(無大括號),並使用'Polymer({is:'my-element', ...)'而不是'Polymer('my-element',{...'。 –

相關問題