Skip to content

Commit db250ed

Browse files
authored
docs: document calling JavaScript module exports via Page
Add Page.executeModuleJs(Class) and the JsModuleInvokers marker to Accessing the Browser Page, and mention the third marker interface alongside JsInvokers and JsSnippets in Calling JavaScript from the Server. Documents vaadin/flow#27492 (`6333b7d89aa0ad3f3d83f53799d449f4767436e0`).
1 parent 527bd05 commit db250ed

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

‎articles/flow/advanced/browser-access.adoc‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ For that reason, the [methodname]`isIOS()` and [methodname]`isIPad()` methods ar
103103
== Executing JavaScript in the Browser
104104

105105
You can use server-side Java to execute JavaScript snippets in the browser.
106-
Declare the JavaScript in a Java interface and run it through [methodname]`Page.executeJs(Class)`: the build collects the declaration into the client bundle, so nothing is compiled from a string in the browser and the call works under a <<{articles}/flow/security/protections/content-security-policy#vaadin-strict-csp,strict content security policy>>.
106+
Declare the JavaScript in a Java interface and run it through [methodname]`Page.executeJs(Class)`, or, to call what a JavaScript module exports, through [methodname]`Page.executeModuleJs(Class)`: the build collects the declaration into the client bundle, so nothing is compiled from a string in the browser and the call works under a <<{articles}/flow/security/protections/content-security-policy#vaadin-strict-csp,strict content security policy>>.
107107

108108
For JavaScript that works on a particular element rather than on the page as a whole, use the Element API instead (see <</flow/component-internals/element-api/calling-javascript#,Calling JavaScript from the Server>>).
109109

@@ -157,6 +157,41 @@ The browser prints the whole page by default. To print only part of it, add a cl
157157
----
158158

159159

160+
[[calling-a-module]]
161+
=== Calling What a JavaScript Module Exports
162+
163+
[methodname]`Page.executeModuleJs(Class)` runs the JavaScript of an interface that extends [interfacename]`JsModuleInvokers` instead of [interfacename]`JsInvokers`. The interface names its module with a [annotationname]`@JsDefinitionImport` that imports no names, and in the JavaScript of each method, `this` is that module. A method without [annotationname]`@JsExpression` calls the export of the same name, so `void initLazy(Element grid);` runs `this.initLazy($0)`.
164+
165+
.Calling the exports of a component connector
166+
[example]
167+
====
168+
[source,java]
169+
----
170+
@JsDefinition
171+
@JsDefinitionImport(from = "./grid-connector.js")
172+
public interface GridConnectorJs extends JsModuleInvokers {
173+
174+
void initLazy(Element grid);
175+
}
176+
----
177+
178+
[source,java]
179+
----
180+
UI.getCurrentOrThrow().getPage()
181+
.executeModuleJs(GridConnectorJs.class)
182+
.initLazy(grid.getElement());
183+
----
184+
====
185+
186+
`this` is the namespace object of the module, the same as `import * as module` gives in JavaScript: it has the named exports, a default export is `this.default`, and it can't be reassigned, so setting a property on it throws a JavaScript `TypeError`. The interface can import more modules by name, the same way a [interfacename]`JsInvokers` interface does, and a method annotated with [annotationname]`@JsExpression` references their exports by those names.
187+
188+
No element is involved in the call, so pass one as an argument where the JavaScript needs it, as `initLazy()` does above. The element arrives as `null` when it isn't attached at the time the call is sent; a method that has to guard against that declares its JavaScript with [annotationname]`@JsExpression`, such as `@JsExpression("if ($0) this.initLazy($0)")`.
189+
190+
Imports aren't inherited: an inherited method's module is the one named by the interface that declares the method, not by the interface it's called through. Each interface that declares methods of the definition, including one the definition only extends, needs its own [annotationname]`@JsDefinitionImport` without names (see <</flow/component-internals/element-api/calling-javascript#declaring-javascript,Declaring JavaScript in Java>> for how annotations and inheritance work across a definition's interfaces).
191+
192+
An interface extends only one of [interfacename]`JsInvokers`, [interfacename]`JsModuleInvokers`, and [interfacename]`JsSnippets`, so a module definition can't also run JavaScript on an element or be passed to [methodname]`JsSnippets.of()`, and it can't be annotated with [annotationname]`@JsStringFallback`, since a string expression has no module to run as `this`.
193+
194+
160195
=== Running a JavaScript Expression
161196

162197
[methodname]`Page.executeJs(String, Object...)` takes the JavaScript as a string.

‎articles/flow/component-internals/element-api/calling-javascript.adoc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The other methods described on this page -- [methodname]`executeJs(String, Objec
2121
[role="since:com.vaadin:vaadin@V25.4"]
2222
== Declaring JavaScript in Java
2323

24-
An interface annotated with [annotationname]`@JsDefinition` declares JavaScript that the build collects into the bundle. It also extends one of two marker interfaces, which decides what its methods declare: [interfacename]`JsInvokers`, covered in this section, for JavaScript that runs when a method is called; or [interfacename]`JsSnippets` for JavaScript that's only passed on as a function (see <<#declaring-snippets,Declaring Snippets>>). An interface can't extend both.
24+
An interface annotated with [annotationname]`@JsDefinition` declares JavaScript that the build collects into the bundle. It also extends one of three marker interfaces, which decides what its methods declare: [interfacename]`JsInvokers`, covered in this section, for JavaScript that runs on an element when a method is called; [interfacename]`JsModuleInvokers` for JavaScript that runs on what a JavaScript module exports (see <</flow/advanced/browser-access#calling-a-module,Calling What a JavaScript Module Exports>>); or [interfacename]`JsSnippets` for JavaScript that's only passed on as a function (see <<#declaring-snippets,Declaring Snippets>>). An interface extends only one of them.
2525

2626
Call the declared JavaScript of a [interfacename]`JsInvokers` interface through [methodname]`Element.executeJs(Class)`, which hands out an implementation of the interface; calling a method of it schedules the JavaScript that the method declares.
2727

@@ -106,7 +106,7 @@ Nothing about the JavaScript is decided at the call site: the build generates on
106106
[NOTE]
107107
The interface is checked when [methodname]`executeJs(Class)` hands out the implementation. It must extend [interfacename]`JsInvokers` and be annotated with [annotationname]`@JsDefinition`. Every method must be annotated with [annotationname]`@JsExpression` and return `void`, [classname]`PendingJavaScriptResult`, or [classname]`JavaScriptResult` with a type argument that's known from the interface (see <<#typed-results,Typed Results>>); a method that returns a snippet type is refused with the method named. A `default` or `static` method is refused, since it's implemented in Java rather than declaring JavaScript to run in the browser.
108108

109-
When the JavaScript isn't about a particular element, declare it the same way and run it through [methodname]`Page.executeJs(Class)`, which works with global browser APIs instead of with an element (see <</flow/advanced/browser-access#declaring-javascript-in-java,Accessing the Browser Page>>).
109+
When the JavaScript isn't about a particular element, declare it the same way and run it through [methodname]`Page.executeJs(Class)`, which works with global browser APIs instead of with an element (see <</flow/advanced/browser-access#declaring-javascript-in-java,Accessing the Browser Page>>). To call what a JavaScript module exports instead, with the module as `this`, extend [interfacename]`JsModuleInvokers` and run it through [methodname]`Page.executeModuleJs(Class)` (see <</flow/advanced/browser-access#calling-a-module,Calling What a JavaScript Module Exports>>).
110110

111111
[[declaring-snippets]]
112112
=== Declaring Snippets
@@ -115,7 +115,7 @@ An interface annotated with [annotationname]`@JsDefinition` that extends [interf
115115

116116
A snippet isn't bound to an element, so get the implementation of its interface from [methodname]`JsSnippets.of(Class)` rather than from [methodname]`executeJs(Class)`. The implementation holds no state, so it can be kept in a constant and shared.
117117

118-
An interface extends exactly one of [interfacename]`JsInvokers` and [interfacename]`JsSnippets`. Passing a [interfacename]`JsSnippets` interface to [methodname]`executeJs(Class)`, or a [interfacename]`JsInvokers` interface to [methodname]`JsSnippets.of()`, is a compile error, since each method accepts only the matching marker type. A method whose return type doesn't match the kind the interface declares is refused with the method named.
118+
An interface extends exactly one of [interfacename]`JsInvokers`, [interfacename]`JsModuleInvokers`, and [interfacename]`JsSnippets`. Passing a [interfacename]`JsSnippets` interface to [methodname]`executeJs(Class)`, or a [interfacename]`JsInvokers` interface to [methodname]`JsSnippets.of()`, is a compile error, since each method accepts only the matching marker type. A method whose return type doesn't match the kind the interface declares is refused with the method named.
119119

120120
The arguments of the Java call are captured and available in the expression as `$0`, `$1`, and so on. The expression is the body of the function, so it has to `return` a value if the caller expects one. The function takes no parameters of its own, unless the method is annotated with [annotationname]`@JsFunctionParameterNames`, which names the parameters that the caller passes to the function when it calls it.
121121

0 commit comments

Comments
 (0)