Annotation Interface JsFunctionParameterNames


@Documented @Target({METHOD,TYPE}) @Retention(RUNTIME) public @interface JsFunctionParameterNames
Names the parameters of the JavaScript function that a JsSnippet evaluates to on the client, so that the JsExpression can use them.

A method returning a JsSnippet involves two calls with different arguments:

  • The Java method is called on the server. Its arguments are captured and the expression refers to them as $0, $1, …, the same way as Element.executeJs(String, Object...) does.
  • The client turns the snippet into a JavaScript function, and whatever the snippet is passed to calls that function later, in the browser, with arguments of its own. The names listed in this annotation are the parameters of that function, and the expression refers to those arguments by these names.
Declaring a name does not make a value available by itself: the name refers to whatever the client side code calling the function passes in that position. The expression is the body of the function, so it must return a value if the caller expects one.

For example, a snippet for a click listener on a link that shares the link with the Web Share API instead of following it. The title and the text are given on the server, and the URL is read from the clicked link through the click event:

 @JsDefinition
 public interface ShareJs extends Serializable {

     @JsExpression("""
             event.preventDefault();
             return navigator.share({
                 title: $0, text: $1, url: event.currentTarget.href });""")
     @JsFunctionParameterNames("event")
     JsFunction share(String title, String text);
 }
 
The build generates the following JavaScript for share. The outer function takes the arguments of the Java call as its parameters $0 and $1. The client calls it once, with the values sent from the server, when it receives the snippet. The inner function is what the snippet evaluates to. Its parameters are the names of this annotation, its body is the expression, and it sees $0 and $1 through the closure:
 function ($0, $1) {
   return function (event) {
     event.preventDefault();
     return navigator.share({
         title: $0, text: $1, url: event.currentTarget.href });
   };
 }
 
The snippet is then passed to code that calls the inner function, such as a definition that registers it as a click listener. The browser calls a listener with the click event, which the snippet sees as event:
 @JsDefinition
 public interface ClickJs extends Serializable {

     @JsExpression("this.addEventListener('click', $0);")
     void onClick(JsFunction listener);
 }

 Anchor link = new Anchor("/reports/q3", "Share the report");
 link.getElement().executeJs(ClickJs.class).onClick(JsSnippets
         .of(ShareJs.class).share("Quarterly report", "Have a look"));
 
All of the JavaScript is declared in the definitions and generated into the bundle, so nothing is compiled from a string in the browser.

Inside the function, this is whatever the caller calls it with.

Without this annotation the function takes no parameters, and the expression is its body all the same:

 @JsExpression("return import('Frontend/report-editor.js');")
 JsFunction loadEditor();
 

A snippet type that is always called in the same way can declare the names once, on the type. The names then apply to all methods that return the type. A method can still declare its own names, and they are used instead. For example, DomEventFilter is always called with the DOM event and the element of the listener, so a filter expression can use both:

 @JsExpression("return event.key === $0 && !element.disabled;")
 DomEventFilter key(String key);
 

The names are part of the generated JavaScript, so a change to them is a change to the function the client runs, as a change to the expression is.

Since:
25.4
See Also:
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The names of the parameters of the function, in the order the caller passes the arguments.
  • Element Details

    • value

      String[] value
      The names of the parameters of the function, in the order the caller passes the arguments. Each is a JavaScript identifier, and none of them is $0, $1, …, which name the captured arguments.
      Returns:
      the parameter names