为 Polymer 自定义元素创建自定义事件
Create a custom event for a Polymer Custom-Element
我一直在尝试创建一个在给定特定条件(更一般情况)的情况下触发的事件,但我还没有找到方法,至少在 Polymer 的官方文档中没有找到处理事件的方法
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)
参考资料
试试这个。 (注意:将事件名称更改为不区分大小写 ['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>
我一直在尝试创建一个在给定特定条件(更一般情况)的情况下触发的事件,但我还没有找到方法,至少在 Polymer 的官方文档中没有找到处理事件的方法
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)
参考资料
试试这个。 (注意:将事件名称更改为不区分大小写 ['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>