2015-12-01 10 views
9

私は基本的に次のコードをES6形式で書いています。PolymerのES6でリスナーを書くには?

listeners: { 
    'neon-animation-finish': '_onNeonAnimationFinish' 
}, 

私は、次のようにプロパティを使用して試してみましたが、_onNeonAnimationFinishコールバックが解雇されることはありません。

class MyElement { 
    get behaviors() { 
     return [Polymer.NeonAnimationRunnerBehavior]; 
    } 

    beforeRegister() { 
     this.is = 'my-element'; 
     this.properties = { 
      name: { 
       type: String 
      } 
     }; 

     this.listeners = { 
      'neon-animation-finish': '_onNeonAnimationFinish' 
     }; 
    } 

正しい方法は何ですか。

+0

この作品かどうかはわかりませんが、あなたは行動のようにそれをやってもらうリスナー(){リターン{」... 『:』 ...}}定義しようとした私が試した – akc42

+1

をそれはあまりにも同じ結果です。 : – Jessica

+0

私はそれもやったでしょう... –

答えて

2

次の要素コードが有効です。説明のためにコード内のコメントを見てください。

<link rel="import" href="bower_components/polymer/polymer.html"> 
<link rel="import" href="bower_components/neon-animation/neon-animation-runner-behavior.html"> 
<link rel="import" href="bower_components/neon-animation/animations/scale-down-animation.html"> 

<dom-module id="my-animatable"> 

    <style> 
    :host { 
     display: inline-block; 
     border-radius: 50%; 
     width: 300px; 
     height: 300px; 
     background: orange; 
    } 
    </style> 

    <template> 
    <content></content> 
    </template> 

</dom-module> 

<script> 
    (function() { 
    'use strict'; 

    var behaviors = Symbol('behaviors'); 

    class MyAnimatable { 
     get behaviors() { 
     if (!this[behaviors]) { 
      this[behaviors] = [Polymer.NeonAnimationRunnerBehavior]; 
     } 

     return this[behaviors]; 
     } 

     //this setter is key to solving this bug. The `NeonAnimationRunnerBehavior` 
     //is an array with two behaviors in it. Polymer allows a 
     //behavior to be an array or an object, because of this it flattens 
     //nested behaviors into a single array containing only objects and 
     //sets it on the prototype, since your implementation didn't have a 
     //setter the flattened behavior was lost!. 
     set behaviors(value) { 
     this[behaviors] = value; 
     } 

     beforeRegister() { 
     this.is = 'my-animatable'; 

     this.properties = { 
      animationConfig: { 
      type: Object, 
      value: function() { 
       return { 
       name: 'scale-down-animation', 
       node: this 
       } 
      } 
      } 
     }; 

     this.listeners = { 
      'neon-animation-finish': '_onNeonAnimationFinish' 
     }; 
     } 

     animate() { 
     this.playAnimation(); 
     } 

     _onNeonAnimationFinish() { 
     alert('_onNeonAnimationFinish'); 
     } 
    } 

    Polymer(MyAnimatable); 
    })(); 
</script> 
+0

ありがとう、これです! – Jessica

関連する問題