Handling User Events in a PolymerTemplate

    To wire an event to a Polymer.Element method you can write a template as:

    HTML

    Clicking on the <button> will now show an alert in the browser. You can listen to any event using the on-*event* syntax, it does not matter if it is a built-in browser event or a custom event from e.g. a web component.

    Server-Side Event Handlers

    To handle a DOM event in a template on the server side you can create a method with the event name and annotate it with @EventHandler.

    So to listen to the handleClick event on the server you could have the template as:

    HTML

    1. <dom-module id="event-handler">
    2. <template>
    3. <button on-click="handleClick">Click me</button>
    4. </template>
    5. <script>
    6. class EventHandler extends Polymer.Element {
    7. static get is() { return 'event-handler' }
    8. }
    9. customElements.define(EventHandler.is, EventHandler);
    10. </script>
    11. </dom-module>

    And define the server class as:

    1. @Tag("event-handler")
    2. @HtmlImport("/com/example/EventHandler.html")
    3. public class EventHandlerPolymerTemplate extends PolymerTemplate<TemplateModel> {
    4. @EventHandler
    5. private void handleClick() {
    6. System.out.println("Received a handle click event");
    7. }
    8. }

    The framework will wire up the client-side event when having the @EventHandler annotation on the method handleClick().

    An event can also include additional information about what has happened, e.g. which mouse button was used for a click event. When you use @EventHandler annotation, all constructor parameters should have an @EventData annotation that tells the framework what data to send from the browser.

    HTML

    To get some extra data on event type and element tag name the server class definition could be built like:

    Java

    1. @Tag("event-handler")
    2. @HtmlImport("/com/example/EventHandler.html")
    3. public class EventDataHandlerPolymerTemplate extends PolymerTemplate<TemplateModel> {
    4. @EventHandler
    5. private void handleClick(@EventData("event.altKey") boolean altPressed,
    6. @EventData("event.srcElement.tagName") String tag,
    7. @EventData("event.offsetX") int offsetX,
    8. @EventData("event.offsetY") int offsetY) {
    9. System.out.println("Event alt pressed: " + altPressed);
    10. System.out.println("Event tag: " + tag.toLowerCase(Locale.ENGLISH));
    11. System.out.println("Click position on element: [" + offsetX + ", "+ offsetY +"]");
    12. }

    Now the client would send the extra information back to the server for , event.srcElement.tagName and the event.offset[X/Y] can then be used like normal variables.

    There is a shorthand for getting model specific item as an object in your event handler. To be able to use it you should define your model (see ) in the template class.

    1. @Tag("model-item-handler")
    2. @HtmlImport("/com/example/ModelItemHandler.html")
    3. public class ModelItemHandlerPolymerTemplate
    4. extends PolymerTemplate<MessagesModel> {
    5. public static class Message {
    6. private String text;
    7. public Message() {
    8. }
    9. public Message(String text) {
    10. this.text = text;
    11. }
    12. public String getText() {
    13. return text;
    14. }
    15. public void setText(String text) {
    16. this.text = text;
    17. }
    18. }
    19. public interface MessagesModel extends TemplateModel {
    20. void setMessages(List<Message> messages);
    21. }
    22. @EventHandler
    23. private void handleClick(@ModelItem Message message) {
    24. System.out.println("Received a message: " + message.getText());
    25. }
    26. }

    Now you can use the template repeater (dom-repeat) (see Using List of Items in a PolymerTemplate with template repeater) and handle click events on the server side with Message as the parameter type.

    HTML

    The method handleClick will be called in the server side with the data identified by event.model.item once the item is clicked.

    So if you have the following model definition and the event handler method:

    Java

    1. private String name;
    2. return name;
    3. }
    4. public void setName(String name) {
    5. this.name = name;
    6. }
    7. }
    8. public interface Model extends TemplateModel {
    9. void setUserInfo(UserInfo userInfo);
    10. }
    11. @EventHandler
    12. private void onClick(
    13. @ModelItem("event.detail.userInfo") UserInfo userInfo) {
    14. System.err.println("contact : name = " + userInfo.getName());
    15. }

    Then the client side code below won’t update the name of the UserInfo bean instance.

    HTML

    1. <dom-module id="contact-handler">
    2. <template>
    3. <input id="name" type="text">
    4. <button on-click="onClick">Send the contact</button>
    5. </template>
    6. </dom-module>
    7. <script>
    8. class ContactHandler extends Polymer.Element {
    9. static get is() { return 'contact-handler' }
    10. onClick(event) {
    11. this.userInfo.name = this.$.name.value;
    12. event.detail = {
    13. userInfo: this.userInfo,
    14. };
    15. }
    16. customElements.define(ContactHandler.is, ContactHandler);
    17. </script>
    18. }