Class DomEventData<T>

java.lang.Object
com.vaadin.flow.dom.DomEventData<T>
Type Parameters:
T - the type of the value
All Implemented Interfaces:
JsSnippet, Serializable

@JsFunctionParameterNames({"event","element"}) public final class DomEventData<T> extends Object implements JsSnippet
A declared JavaScript expression. When a DOM event occurs, the browser runs the expression and sends its value to the server together with the event. Use it with DomListenerRegistration.addEventData(DomEventData).

To declare event data, add a method that returns this type to a JsDefinition interface. To create event data, call that method on the object that JsSnippets.of(Class) returns.

The expression is the body of a function. The function gets two parameters: the DOM event, and the element that the listener is added to. It returns the value to send. The arguments of the method call are captured, and the expression uses them as $0, $1, and so on.

In the listener, read the value with DomEvent.getEventData(DomEventData). The value has the type that the method declares. For DomEventData<Element>, the expression returns a DOM node, and the value is the closest Element of the server that is an ancestor of the node or the node itself, see DomEvent.getEventDataElement(DomEventData):

 @JsDefinition
 public interface ClipboardJs extends Serializable {

     @JsExpression("return event.clipboardData?.getData($0) || null;")
     DomEventData<String> clipboardData(String mimeType);
 }

 static final ClipboardJs CLIPBOARD = JsSnippets.of(ClipboardJs.class);

 DomEventData<String> pasted = CLIPBOARD.clipboardData("text/plain");
 field.getElement()
         .addEventListener("paste",
                 event -> handle(event.getEventData(pasted)))
         .addEventData(pasted);
 

The build adds the expression to the bundle, so the browser does not compile any JavaScript from a string. Event data from the same method shares one function in the browser, also when the arguments are different. Each one still sends its own value: a listener can have both clipboardData("text/plain") and clipboardData("text/html"), and DomEvent.getEventData(DomEventData) returns the value of the one that you pass to it.

When all arguments are plain values (strings, numbers, booleans, characters, enums or null), the settings of the listener are stored in a cache that the server and the browser keep for the lifetime of the UI. Each different value adds one entry to this cache, and entries are never removed. So avoid a captured value that is different for each element, such as a row id on every row of a long list. Read such a value from the element instead, for example from an attribute.

The captured values are checked when the event data is created. If a value cannot be sent to the browser, creating the event data fails.

The captured values are encoded each time the settings of the listener are sent to the browser. This happens when the listeners of the element change, and when the whole UI is sent again, for example after a page refresh with @PreserveOnRefresh. A captured element or component is sent with the id that it has at that time.

So a captured element or component must be attached before the settings are sent: in the same round trip in which the event data is added to a listener, or earlier. If it is not attached at that time, the browser gets null for it until the settings are sent again. The browser also gets null for a captured element or component while it is detached. A declared expression that uses such a captured value must handle null.

DomEvent.getEventData(DomEventData) finds the value by the method and the arguments of the event data that you pass to it. If all arguments are plain values (strings, numbers, booleans, characters, enums or null), you can pass any event data created with the same method and equal arguments. With any other argument, such as an element, a record or a list, pass the same instance that you added to the listener. Another instance gets null, so keep the instance in a variable.

Since:
25.4
See Also:
  • Method Details

    • ofDeclaredCall

      public static <T> DomEventData<T> ofDeclaredCall(JsCall call, Type returnType)
      Creates event data for the given call of a JsDefinition method. The method must return event data.

      For internal use only. May be renamed or removed in a future release. Create event data by calling the relevant method on the implementation returned by JsSnippets.of(Class).

      Type Parameters:
      T - the type of the value
      Parameters:
      call - the call of a method that returns event data, not null
      returnType - the generic return type of the called method. The value is read as this type. Not null
      Returns:
      new event data, not null
      Throws:
      IllegalArgumentException - if the called method does not declare a JsSnippet
      RuntimeException - if any argument of the call cannot be encoded to be sent to the client
    • getDeclaredCall

      public JsCall getDeclaredCall()
      Gets the call of the JsDefinition method that declared this event data. The call tells which function of the bundle to run.

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

      Returns:
      the declaring call, not null
    • getSnippetKey

      public String getSnippetKey()
      Gets the key that identifies this snippet: its function and its captured values. It does not change after the snippet is created. See JsCall.createSnippetKey().

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

      Returns:
      the key of the snippet, not null
    • isElementValue

      public boolean isElementValue()
      Checks whether the value of this event data is an Element. The browser then sends the closest element that the server knows instead of the DOM node that the expression returns.

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

      Returns:
      true if the value type is Element, false otherwise