You export a method in Java using @ClientCallable, for example:
@ClientCallable
private void myFunction(){
//do something on the server-side
}
… and then call it on the client side by using the $server object:
element.$server.myFunction();
The name of the function exported on the client-side is the same name of the method declared on the server side.
Okay this works well for void methods. But how can i send data to the client side? In my case i have an addon where the serverside computes some data. This data should be requestable vis javascript on client side. Is this possible?
On the java side set the id of the element with the @ClientCallable method.
Call setId(“myelement”) on your com.vaadin.flow.component.Component element.
On the client side get the element in java script with:
var element = document.getElementById("myelement");
This only works reliably if you only have one element with the id “myelement” on the page.
Bastian Stegmann:
On the java side set the id of the element with the @ClientCallable method.
Call setId(“myelement”) on your com.vaadin.flow.component.Component element.
On the client side get the element in java script with:
var element = document.getElementById("myelement");
This only works reliably if you only have one element with the id “myelement” on the page.
In my case I dont have any element on the java side to set the ID, I just want to send a string from JS and print it inside mu java method.