1

In Vaadin it's possible to register a JavaScript function for example like this:

JavaScript.getCurrent().addFunction("openObj", new JavaScriptFunction() {
    private static final long serialVersionUID = 9167665131183664686L;

    @Override
    public void call(JsonArray arguments) {
        if (arguments.length() != 1) {
            Notification.show("Wrong arguments for openObj: " + arguments.asString());
            return;
        }
        openObject(arguments.get(0).asString());
    }
});

Is it somehow possible to register a function which has a return value?

2 Answers 2

3

You could work round this by calling back to another JavaScript method.

JavaScript.getCurrent().addFunction("openObj", new JavaScriptFunction() {
    private static final long serialVersionUID = 9167665131183664686L;

    @Override
    public void call(JsonArray arguments) {
        if (arguments.length() != 1) {
            Notification.show("Wrong arguments for openObj: " + arguments.asString());
            return;
        }
        String val = openObject(arguments.get(0).asString());
        JavaScript.getCurrent().execute("myMethod('" + val + "');");
    }
});

Then in your JS when you call the openObj function could look something like this:

function doStuff(obj){
    openObj(obj);
}

function myMethod(val)
{
    alert(val);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I used your workaround to store the content in a JavaScript variable, which I then access from the HTML. Was a bit tricky because at the time I access the variable, vaadin has not registered it. So I needed a timeout function. Not the cleanest solution but it works thanks for the hint.
0

This is the JavaDoc for the JavaScriptFunction#call(JsonArray) method, which explains that you cannot have a return value:

     /**
     * Invoked whenever the corresponding JavaScript function is called in the
     * browser.
     * <p>
     * Because of the asynchronous nature of the communication between client
     * and server, no return value can be sent back to the browser.
     * 
     * @param arguments
     *            an array with JSON representations of the arguments with which
     *            the JavaScript function was called.
     */
    public void call(JsonArray arguments);

1 Comment

I was hoping for a workaround or maybe some other approach. Maybe there is other API than JavaScriptFunction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.