Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit b7304b6

Browse files
author
Arthur Evans
committed
Merge pull request #710 from Polymer/imperative_events
Fix for bug #546, added docs for event listeners on the host element.
2 parents fca5ffe + 3e65e42 commit b7304b6

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/polymer/polymer.md

+44
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,50 @@ Some things to notice:
765765
* `inDetail`: A convenience form of `inEvent.detail`.
766766
* `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.
767767

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.
811+
768812
### Observing properties {#observeprops}
769813

770814
#### Changed watchers {#change-watchers}

0 commit comments

Comments
 (0)