2015-12-01 29 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

我不知道这是否正常工作或没有,但你尝试过做类似的行为,并定义一个get听众(){回报{” ...‘:’...}} – 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