Annotation Interface JsCalledWith


@Documented @Target({METHOD,TYPE}) @Retention(RUNTIME) public @interface JsCalledWith
The parameters a JsSnippet is called with by whatever invokes it on the client, which the declared expression references by name.

The JsExpression of the annotated method becomes the body of a function taking these parameters, and that function is the value of the snippet:

 @JsDefinition
 public interface LoggerJs extends Serializable {

     @JsExpression("console.log($0, value);")
     @JsCalledWith("value")
     JsFunction createLogger(String prefix);
 }

 @JsDefinition
 public interface ClickJs extends Serializable {

     @JsExpression("this.addEventListener('click', () => $0('clicked'));")
     void onClick(JsFunction handler);
 }

 button.getElement().executeJs(ClickJs.class)
         .onClick(JsSnippets.of(LoggerJs.class).createLogger("Button:"));
 

The arguments of the Java call are captured on the server and referenced as $0, $1, …, the same way as by Element.executeJs(String, Object...). The names declared here are what the invoker passes when it calls the function. Inside the function, this is whatever the invoker 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.

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, in the order the invoker passes them.
  • Element Details

    • value

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