javascriptjavavaadin

Vaadin execute multiple lines of javascript code


I want to call a function with a callback that contains multiple lines of javascript code from a vaadin component.

When I use getElement().executeJs(...) or UI.getCurrent().getPage().executeJs(...) and I pass my code, only the first line is executed.

Here is an example in which only the first alert is called. Is there a way to call all lines from java?

@NpmPackage(value = "leaflet-image", version = "0.4.0")
@JsModule("leaflet-image/leaflet-image.js")
@Tag("leaflet-image")
public class LLeafletImage extends Div {

    private static final long serialVersionUID = 1L;

    public void print(LMap map) {
        
        UI.getCurrent().getPage().executeJs("alert('-1');"
            + "leafletImage($0, function(err, canvas) {"
            + "alert('0');"
            + "var img = document.createElement('img');"
            + "var dimensions = map.getSize();"
            + "img.width = dimensions.x;"
            + "img.height = dimensions.y;"
            + "img.src = canvas.toDataURL();"
            + "document.getElementById('images').innerHTML = '';"
            + "document.getElementById('images').appendChild(img);"
            + "});"
            + "alert('2');", map.clientComponentJsAccessor());
    }
}

Solution

  • I figured out that executeJs() can and does execute multiple lines of javascript code. But if an error occurs in the middle of the execution, the script is interrupted.