Class CallMethodAction

java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.CallMethodAction
All Implemented Interfaces:
Serializable

public class CallMethodAction extends Action
Calls a JavaScript method on a target element when the bound trigger fires. Pure client-side — no server round-trip; the method's return value is discarded.

Each argument is supplied as an Action.Input: a literal captured at build time, the current value of a DOM property, an event-scoped expression, or any other input. The rendered JS evaluates target[method](arg0, arg1, ...) where each argN is the input's value at the moment the trigger fires.

Common idioms:

  • Focus an input on click: new CallMethodAction(input, "focus")
  • Scroll an element into view: new CallMethodAction(panel, "scrollIntoView")
  • Select all text: new CallMethodAction(field, "select")
  • Click another element: new CallMethodAction(other, "click")
  • Submit a form: new CallMethodAction(form, "requestSubmit")
  • Play / pause a media element: new CallMethodAction(video, "play") / new CallMethodAction(video, "pause")
  • Pass an options object built on the server: new CallMethodAction(panel, "scrollIntoView", new LiteralInput<>(Map.of("behavior", "smooth")))

For methods that return a Promise, the promise is created and dropped — the JS engine may log an unhandled-rejection warning if the promise rejects. Use PromiseAction based actions when you need to observe the outcome on the server.

Server-side state is not updated by this action; the call lives in the browser until the next sync from the client (if any).

For internal use only. May be renamed or removed in a future release.

See Also:
  • Constructor Details

    • CallMethodAction

      public CallMethodAction(Component target, String methodName, Action.Input<?>... arguments)
      Creates an action that, when the trigger fires, evaluates each input on the client and calls target[methodName](...) with those values.
      Parameters:
      target - the component on whose root element the method is invoked, not null
      methodName - the JS method name (e.g. "focus", "scrollIntoView"), not null
      arguments - inputs producing the method's positional arguments, in order; may be empty for a no-arg call. Neither the array nor any element may be null
  • Method Details

    • toJs

      protected JsFunction toJs(Trigger trigger)
      Description copied from class: Action
      Builds the JsFunction that runs this action when the surrounding trigger fires. The returned function takes one runtime argument named event (declared by the framework when it composes the trigger handler); subclasses do not declare argument names themselves.

      The body is one statement. To embed a value produced on the client, capture an Action.Input's JsFunction as a capture and invoke it inside the body as $N(event).

      Specified by:
      toJs in class Action
      Parameters:
      trigger - the surrounding trigger this render is for, not null
      Returns:
      the action's JS function, not null