user4375474
user4375474

Reputation:

Using JavaScript in Vaadin Java Application

I want to use the Kurento WebRTC javascript client in my Java web application.

But I even cant manage to call a simple javascript function in my Java application. What am I missing?

var connector = this;

connector.test = function() {
    alert('Hello world!');
}

Here comes the Java Code:

import com.vaadin.ui.AbstractJavaScriptComponent;


@JavaScript({"rtc.js", "jquery-1.12.3.min.js"})
public class VideoCall extends AbstractJavaScriptComponent {

    public VideoCall() {

    }

    public void testMethod() {
        callFunction("test");
        //Page.getCurrent().getJavaScript().execute("alert('Hello world!')");
        System.out.println("testMethod executed!");
    }

}

When I uncommend

Page.getCurrent().getJavaScript().execute("alert('Hello world!')");

I get a window with the message "Hello world!". When I change test to foo as argument in callFunction I dont get an error message, although foo doesnt exist as function. Why does the code not work?

Upvotes: 0

Views: 169

Answers (1)

Leif Åstrand
Leif Åstrand

Reputation: 8001

I assume the first code snippet you've shown there is the rtc.js file that you include using @JavaScript. What's missing from that file is that it should register itself as a connector for that particular Java class.

The file should look like this:

window.java_package_name_VideoCall = function() {
  var connector = this;

  connector.test = function() {
    alert('Hello world!');
  }
}

where java_package_name is the the package name of the VideoCall class with . replaced with _.

Upvotes: 1

Related Questions