Annotation Interface JsDefinition


@Documented @Target(TYPE) @Retention(RUNTIME) public @interface JsDefinition
Marks an interface that declares JavaScript, so that the build and the development server find it. Every annotated interface is collected into the generated bundle as a function per method, so the JavaScript an application can invoke from the server is known before it runs and the client never has to build a function from a string. That is what keeps a server-initiated call compatible with a content security policy that does not allow unsafe-eval.

What the interface declares is up to the marker interface it extends: an interface that extends JsInvokers declares, with JsExpression, JavaScript that runs when a method is called through Element.executeJs(Class), and one that extends JsSnippets declares snippets, obtained through JsSnippets.of(Class). An interface that is used through either of them without this annotation is refused, since its JavaScript would be missing from the bundle.

The annotation, rather than the marker interface, is what the interface is found by, since a servlet container is not required to report the interfaces that extend a type listed in @HandlesTypes, while it does report the types annotated with one listed there.

 @JsDefinition
 public interface ScrollJs extends JsInvokers {

     @JsExpression("this.scrollTop = $0;")
     void scrollTo(int top);
 }
 

Which marker to extend

Extend JsInvokers when the server wants JavaScript to run now, optionally with a result. Its methods return void, JavaScriptResult<T> or PendingJavaScriptResult. For an implementation obtained from Element.executeJs(Class), this is the element; one obtained from Page.executeJs(Class) runs on nothing in particular.

Extend JsSnippets when you need a function to pass on: as an argument of an invoker method, as a DomEventFilter or DomEventData of a listener, or as a snippet type that a component accepts. Its methods return JsFunction, JsSnippet, DomEventFilter, DomEventData or an interface that extends JsSnippet. They run nothing when called, and this is whatever calls the function. Implementations are obtained from JsSnippets.of(Class).

One interface can not do both. Declare the two kinds in two interfaces, as in the example of JsSnippets. JsFunctionParameterNames applies only to the methods of a JsSnippets interface.

Inheritance

A definition can extend other interfaces, and the methods it inherits are part of it: they are collected into the bundle with the definition and can be called through its implementation, as if the definition declared them itself.

Annotations on interfaces are not inherited, so every interface that is passed to Element.executeJs(Class), Page.executeJs(Class) or JsSnippets.of(Class) needs its own @JsDefinition, even when an interface it extends has one. An interface that is only extended, and never passed to any of them itself, does not need the annotation:

 // Only extended, so not annotated
 public interface ScrollableJs extends JsInvokers {

     @JsExpression("this.scrollTop = $0;")
     void scrollTo(int top);
 }

 @JsDefinition
 public interface ListJs extends ScrollableJs {

     @JsExpression("this.scrollTop = this.scrollHeight;")
     void scrollToEnd();
 }

 element.executeJs(ListJs.class).scrollTo(100);
 
A sub-interface can override an inherited method with another JsExpression. The JavaScript of the override is what runs for that method when it is called through the sub-interface; the inherited one still runs through the interface that declares it.
Since:
25.4
See Also: