Annotation Interface JsDefinition
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.
A production build that optimizes the bundle, which is the default, collects
only the definitions it can reach from the entry points of the application,
the same way it collects @JsModule: a definition is reached when a
reached class refers to it, such as by passing it to
Element.executeJs(Class). The definitions in the flow-server jar and
those annotated with JsStringFallback are always collected. Any other
definition that is only used through reflection, or from code that no route
or other entry point leads to, is left out of that bundle, and a call to it
fails in the browser. Name it in
Uses on a class that is reached
to keep it. Development mode collects every definition.
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
ExtendJsInvokers 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: