You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 29, 2019. It is now read-only.
Copy file name to clipboardExpand all lines: docs/polymer/polymer.md
+44
Original file line number
Diff line number
Diff line change
@@ -765,6 +765,50 @@ Some things to notice:
765
765
*`inDetail`: A convenience form of `inEvent.detail`.
766
766
*`inSender`: A reference to the node that declared the handler. This is often different from `inEvent.target` (the lowest node that received the event) and `inEvent.currentTarget` (the component processing the event), so {{site.project_title}} provides it directly.
767
767
768
+
#### Imperative event mapping
769
+
770
+
Alternatively, you can add event handlers to a {{site.project_title}} element imperatively.
771
+
772
+
**Note:** In general, the declarative form is preferred.
773
+
{: .alert .alert-info}
774
+
775
+
<polymer-element name="g-button">
776
+
<template>
777
+
<button>Click Me!</button>
778
+
</template>
779
+
<script>
780
+
Polymer({
781
+
eventDelegates: {
782
+
up: 'onTap',
783
+
down: 'onTap'
784
+
},
785
+
onTap: function(event, detail, sender) {
786
+
...
787
+
}
788
+
});
789
+
</script>
790
+
</polymer-element>
791
+
792
+
The example adds event listeners for `up` and `down` events
793
+
to the {{site.project_title}} element called `g-button`.
794
+
The listeners are added to the host element rather than to individual
795
+
elements it contains.
796
+
These listeners handle events on the host element
797
+
in addition to events that bubble up from within it.
798
+
This code is equivalent
799
+
to adding an <code>on-<em>event</em></code>
800
+
handler directly on a `<polymer-element>`.
801
+
802
+
The relationship between the <code>on-<em>event</em></code> attribute
803
+
and the `eventDelegates` object
804
+
is analogous to the relationship between the
805
+
`attributes` attribute and the `publish` object.
806
+
807
+
The keys within the `eventDelegates` object are the event names to listen for.
808
+
The values are the callback function names, here `onTap`.
809
+
Event handler functions defined imperatively
810
+
receive the same arguments as those defined declaratively.
0 commit comments