Annotation Interface JsCalledWith


@Documented @Target(METHOD) @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 no function is wrapped around the expression, which then has to return the function itself, for an invoker whose calling convention a list of parameters can not express, or for setup that runs once for the function rather than on every call:

 @JsExpression("return ({ point }) => `${point.y} ${$0}`;")
 JsFunction formatTooltip(String unit);
 

The expression itself has no this, as the generated module runs in strict mode. The function it returns gets the this of the invoker only when it is a function rather than an arrow function.

The client runs such an expression on the first call of the function rather than when it receives the snippet, so it never runs while the parameters of an invocation are decoded, and an expression that does not return a function fails with an error that names the method on that first call. What the invoker gets is a function that forwards to the returned one, so its length is 0.

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.

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