This Stack Overflow answer suggests using <iron-signals>
to broadcast an event down the DOM tree to a custom element.
Below, I ask a different question.
Question
How do I:
- pass an event down to a direct child node (custom element)
- from a parent (custom element)
- without using
<iron-signals>
?
Code
This is what I have so far. But it doesn't work.
parent-element.html<dom-module id="parenet-element">
<template is="dom-bind">
<child-element></child-element>
<paper-button on-tap="_handleTap"></paper-button>
</template>
</dom-module>
<script>
(function(){
Polymer({
is: 'parenet-element',
_handleTap: function() {
this.fire("my-event");
}
});
})();
</script>
child-element.html
<dom-module id="child-element">
...
</dom-module>
<script>
(function(){
Polymer({
is: 'child-element',
listeners: {
"my-event": "foo"
},
foo: function(){
// Do stuff
}
});
})();
</script>
You definitely can. Without
iron-signals
you've got three options (that I currently know of):document
firing that event (but I think this is bad)Here's an example
With Polymer 1.2.4 as documented here we can use
fire
method options and force a child node (while still inside a parent element) to fire (and listen first of course) an event:We fired a custom event from an element but the emitter is the
my-child-element
so in there we can attach a listener in thelisteners
object. We also prevent the event bubbling so this event won't move upwards following the parents elements path. A typical listener could be:Polymer Slack Site