Integrating JavaScript Components and Extensions
There are many kinds of component libraries for JavaScript. In the following, we present a simple library that provides one object-oriented JavaScript component. We use this example later to show how to integrate it with a server-side Vaadin component.
The example library includes a single MyComponent component, defined in mylibrary.js.
When used in an HTML page, the library would be included with the following definition:
src="mylibrary.js"></script>
You could then use it anywhere in the HTML document as follows:
<!-- Placeholder for the component -->
<div id="foo"></div>
<!-- Create the component and bind it to the placeholder -->
<script type="text/javascript">
window.foo = new mylibrary.MyComponent(
document.getElementById("foo"));
window.foo.click = function () {
alert("Value is " + this.getValue());
}
</script>
A JavaScript Component Example
To begin integrating such a JavaScript component, you would need to sketch a bit how it would be used from a server-side Vaadin application. The component should support writing the value as well as listening for changes to it.
final MyComponent mycomponent = new MyComponent();
// Set the value from server-side
mycomponent.setValue("Server-side value");
// Process a value input by the user from the client-side
mycomponent.addValueChangeListener(
new MyComponent.ValueChangeListener() {
@Override
Notification.show("Value: " + mycomponent.getValue());
}
});
A JavaScript component extends the AbstractJavaScriptComponent, which handles the shared state and RPC for the component.
package com.vaadin.book.examples.client.js;
@JavaScript({"mylibrary.js", "mycomponent-connector.js"})
public class MyComponent extends AbstractJavaScriptComponent {
public interface ValueChangeListener extends Serializable {
void valueChange();
}
ArrayList<ValueChangeListener> listeners =
new ArrayList<ValueChangeListener>();
public void addValueChangeListener(
ValueChangeListener listener) {
listeners.add(listener);
}
public void setValue(String value) {
getState().value = value;
}
public String getValue() {
return getState().value;
}
@Override
protected MyComponentState getState() {
}
Notice later when creating the JavaScript connector that its name must match the package name of this server-side class.
The shared state of the component is as follows:
If the member variables are private, you need to have public setters and getters for them, which you can use in the component.
A JavaScript connector is a function that initializes the JavaScript component and handles communication between the server-side and the JavaScript code.//TOD Clarify - code?
The Vaadin client-side framework adds a number of methods to the connector function. The this.getElement() method returns the HTML DOM element of the component. The this.getState() returns a shared state object with the current state as synchronized from the server-side.
window.com_vaadin_book_examples_client_js_MyComponent =
function() {
// Create the component
var mycomponent =
new mylibrary.MyComponent(this.getElement());
// Handle changes from the server-side
this.onStateChange = function() {
mycomponent.setValue(this.getState().value);
};
// Pass user interaction to the server-side
var self = this;
mycomponent.click = function() {
self.onClick(mycomponent.getValue());
};
};
In the above example, we pass user interaction using the JavaScript RPC mechanism, as described in the next section.
User interaction with the JavaScript component has to be passed to the server-side using an RPC (Remote Procedure Call) mechanism. The JavaScript RPC mechanism is almost equal to regular client-side widgets, as described in “RPC Calls Between Client- and Server-Side”.
Let us begin with the RPC function registration on the server-side. RPC calls are handled on the server-side in function handlers that implement the JavaScriptFunction interface. A server-side function handler is registered with the addFunction() method in AbstractJavaScriptComponent. The server-side registration actually defines a JavaScript method that is available in the client-side connector object.
Continuing from the server-side MyComponent example we defined earlier, we add a constructor to it that registers the function.
public MyComponent() {
addFunction("onClick", new JavaScriptFunction() {
@Override
public void call(JsonArray arguments) {
getState().setValue(arguments.getString(0));
for (ValueChangeListener listener: listeners)
listener.valueChange();
}
});
An RPC call is made simply by calling the RPC method in the connector. In the constructor function of the JavaScript connector, you could write as follows (the complete connector code was given earlier):
You can pass anything that is valid in JSON notation in the parameters.