Call Java Method from Polymer Template

Hi *,

I wanne do something like this:

<dom-module id="hello-world">
    <template>
        <div>
            <paper-input id="inputId" value="{{userInput}}"></paper-input>
            <button id="helloButton" on-click="sayHello">Say hello</button>
            <div id="greeting">[[greeting]
]</div>
        </div>
    </template>
    <script>
        class HelloWorld extends Polymer.Element {
            static get is() {
                return 'hello-world'
            }
			sayHello() {
				doSomething();
				sayHelloIntern();
				doSomethingElse();
			}
        }
        customElements.define(HelloWorld.is, HelloWorld);
    </script>
</dom-module>

Java:

@Tag("hello-world")
@HtmlImport("/src/HelloWorld.html")
public class HelloWorld extends PolymerTemplate<HelloWorldModel> {
    private static final String EMPTY_NAME_GREETING = "Please enter your name";

    /**
     * Creates the hello world template.
     */
    public HelloWorld() {
        setId("template");
        getModel().setGreeting(EMPTY_NAME_GREETING);
    }

    @EventHandler
    private void sayHelloIntern() {
		doMoreStuff();
    }
}

Is this possible?

Basically yes, you can find detailed information here: https://vaadin.com/docs/v12/flow/polymer-templates/tutorial-template-event-handlers.html

What I see a problem in your example, is that your Java method is named sayHelloIntern(), but it should be just sayHello()

You can call a server method in javascript this way:
this.$server.sayHelloIntern();

and change your javacode:

@ClientCallable
private void sayHelloIntern() {
}

I don’t think that doSomethingElse will be executed after sayHelloIntern. (I think java and javascript will be executed asynchronously).

If you want to call it synchronously you can call doSomethingElse in your java code:
https://vaadin.com/docs/v12/flow/element-api/client-server-rpc.html

Hi Jean-Christophe,

thanks a lot. This was exactly what I needed do.

Greetz,
Lars