Interface JsInvokers

All Superinterfaces:
Serializable
All Known Subinterfaces:
AbstractNodeStateProvider.AttachExistingElementJs, AbstractRouteNotFoundError.CopilotJs, DndUtil.DndJs, Element.CallFunctionJs, Element.ElementJs, Element.ScrollIntoViewJs, ElementJsInitializerRegistration.InitializerJs, ExtendedClientDetails.ClientDetailsJs, Focusable.FocusJs, HasTextSelection.TextSelectionJs, History.HistoryJs, Hotswapper.RefreshUiJs, IFrame.ReloadJs, Page.NavigationJs, Page.PageJs, PublishedServerEventHandlerRpcHandler.PromiseJs, ScreenOrientation.ScreenOrientationJs, ShortcutRegistration.ShortcutJs, SignalInput.MirrorJs, UI.UiJs, UidlRequestHandler.MprPushStateJs, UIInternals.TitleJs, WakeLock.WakeLockJs, WebComponent.CustomEventJs, WebComponentBootstrapHandler.StylesheetJs, WebComponentUI.ThemeAttributesJs, WebPushJs, WebStorage.WebStorageJs

public interface JsInvokers extends Serializable
Marks an interface whose methods declare, with JsExpression, the JavaScript they run when they are called. A method without the annotation calls the method of the same name on the element. An implementation is obtained from Element.executeJs(Class) or Page.executeJs(Class), and calling a method of it schedules the declared JavaScript to run on the client.

A call through Page.executeJs(Class) runs on no element, so it can not call a method on one. An interface that has a method without JsExpression that calls the method of the same name on the element, declared in the interface itself or inherited from another interface that extends JsInvokers, can only be used with Element.executeJs(Class): Page.executeJs(Class) throws an IllegalArgumentException for it. See JsCall.declaredExpression(java.lang.reflect.Method) for which methods call the element method.

An interface that is passed to Element.executeJs(Class) or Page.executeJs(Class) must itself be annotated with JsDefinition, since annotations on interfaces are not inherited. The annotation is what makes the interface findable during the build: its JavaScript is collected into the generated bundle, so 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. See JsDefinition for how a definition that extends another interface is declared.

An interface whose methods only answer with snippets extends JsSnippets instead. An interface can not extend both. Both are Serializable, so an implementation can be kept in a field of a component.

 @JsDefinition
 public interface ScrollJs extends JsInvokers {

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

     // Runs this.scrollIntoView($0) on the element
     void scrollIntoView(JsonNode options);
 }

 element.executeJs(ScrollJs.class).scrollTo(100);
 
Since:
25.4
See Also: