Class JsFunction
- All Implemented Interfaces:
Serializable
Element.executeJs(String, Object...) as a parameter and reified on
the client as an actual callable JS function.
The body is a JavaScript function body. Captures are
referenced positionally as $0, $1, … (the same naming
convention as executeJs parameters). Additional named runtime
arguments can be declared with withArguments(String...): the
materialised function then accepts them positionally and the body references
them by name. On the client the value materialises as a function with the
captures pre-bound, so the parent executeJs can invoke it as
$N(arg1, arg2, ...) without ever concatenating user-supplied
JavaScript with framework boilerplate.
Captures may be any value accepted as a parameter to
executeJs, including
Element (attached elements arrive as DOM nodes, detached ones as
null) and nested JsFunction instances. Capture types are
validated when the value is created.
Inside the body, this is whatever the caller of the materialised
function chooses (i.e. it follows normal JavaScript call semantics –
not the host element). To use the host element from within the body, pass it
as a capture.
- Author:
- Vaadin Ltd
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionThe names of the runtime arguments declared viawithArguments(String...), in positional order.getBody()The JavaScript function body.The captured values, in declaration order.static JsFunctionCreates a JavaScript function value with the given body and captured parameters.withArguments(String... argumentNames) Returns a copy of this function that declares the given names as positional runtime arguments.
-
Method Details
-
of
Creates a JavaScript function value with the given body and captured parameters.- Parameters:
body- the JavaScript function body, with captures referenced as$0,$1, …; notnullcaptures- the values to capture; each must be a type supported as a parameter toElement.executeJs(String, Object...)- Returns:
- a new
JsFunctioninstance - Throws:
IllegalArgumentException- if any capture has a type that cannot be sent to the client
-
withArguments
Returns a copy of this function that declares the given names as positional runtime arguments. The materialised function accepts these arguments at call time, and the body references them by name.Example:
JsFunction alerter = JsFunction.of("alert(message);") .withArguments("message"); element.executeJs("$0('Hello')", alerter);- Parameters:
argumentNames- the names of runtime arguments, in positional order; each must be a valid JavaScript identifier- Returns:
- a new
JsFunctionwith the given argument names - Throws:
IllegalStateException- if argument names have already been set on this instance
-
getBody
The JavaScript function body.- Returns:
- the body string
-
getCaptures
The captured values, in declaration order.- Returns:
- an unmodifiable list of captures
-
getArgumentNames
The names of the runtime arguments declared viawithArguments(String...), in positional order.- Returns:
- an unmodifiable list of argument names; empty if none were declared
-