admin管理员组文章数量:1289880
Suppose the following is called from within a Polymer element:
this.fire("reset-counters");
.
Will the reset-counters
event be published to all elements that listen for that event or is in heard within the element that called this.fire()
only?
Suppose the following is called from within a Polymer element:
this.fire("reset-counters");
.
Will the reset-counters
event be published to all elements that listen for that event or is in heard within the element that called this.fire()
only?
- to all listeners added, like standard custom events. – Supersharp Commented Sep 2, 2016 at 17:38
1 Answer
Reset to default 10By default this.fire()
raises a bubbling even which will be handled by all elements up the DOM tree. Like most events in the browser.
Polymer does however offer an API similar to the native events API, The fire
method takes three parameters: event name, details object and options object. In options, set bubbles: false
to forbid the event from being pushed up the DOM tree.
See the example below to see how only the immediate listener fires when you click the second button.
Polymer({
is: 'my-elem',
bubbling: function() {
this.fire('my-event', 'bubbling');
},
nonbubbling: function() {
this.fire('my-event', 'nonbubbling', {
bubbles: false
});
}
});
<!DOCTYPE html>
<html>
<head>
<base href="https://polygit/ponents/">
<script src="webponentsjs/webponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import"/>
</head>
<body>
<div>
<my-elem></my-elem>
</div>
<dom-module id="my-elem">
<template>
<input type="button" value="fire bubbling" on-tap="bubbling" />
<input type="button" value="fire non-bubbling" on-tap="nonbubbling" />
</template>
</dom-module>
<script>
document.querySelector('my-elem')
.addEventListener('my-event', handle('my-elem'));
document.querySelector('div')
.addEventListener('my-event', handle('div'));
document
.addEventListener('my-event', handle('document'));
function handle(elem) {
return function(e) {
console.log(e.detail + ' handled on ' + elem);
};
}
</script>
</body>
</html>
本文标签: javascriptDoes Polymer fire() publish the event globallyStack Overflow
版权声明:本文标题:javascript - Does Polymer fire() publish the event globally? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741465193a2380271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论