You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit db250ed
Browse filesBrowse the repository at this point in the historyBrowse files
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`).
Copy file name to clipboardExpand all lines: articles/flow/advanced/browser-access.adoc
+36-1Lines changed: 36 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -103,7 +103,7 @@ For that reason, the [methodname]`isIOS()` and [methodname]`isIPad()` methods ar
103
103
== Executing JavaScript in the Browser
104
104
105
105
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>>.
107
107
108
108
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>>).
109
109
@@ -157,6 +157,41 @@ The browser prints the whole page by default. To print only part of it, add a cl
157
157
----
158
158
159
159
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
+
160
195
=== Running a JavaScript Expression
161
196
162
197
[methodname]`Page.executeJs(String, Object...)` takes the JavaScript as a string.
Copy file name to clipboardExpand all lines: articles/flow/component-internals/element-api/calling-javascript.adoc
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ The other methods described on this page -- [methodname]`executeJs(String, Objec
21
21
[role="since:com.vaadin:vaadin@V25.4"]
22
22
== Declaring JavaScript in Java
23
23
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.
25
25
26
26
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.
27
27
@@ -106,7 +106,7 @@ Nothing about the JavaScript is decided at the call site: the build generates on
106
106
[NOTE]
107
107
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.
108
108
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>>).
110
110
111
111
[[declaring-snippets]]
112
112
=== Declaring Snippets
@@ -115,7 +115,7 @@ An interface annotated with [annotationname]`@JsDefinition` that extends [interf
115
115
116
116
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.
117
117
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.
119
119
120
120
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.
0 commit comments