What is an interaction event?
- An event is a channel through which the view layer communicates with the logical layer.
- An event can pass a user's action to the logical layer and trigger respective event handlers.
- An event can be bound with a component. When the event is triggered, the specified event handler is executed at the logic layer.
- An event object can carry additional information such as
id,dataset, andtouches.
Event types
Events are divided into bubbling events and non-bubbling ones.
- Bubbling event: When an event is triggered on a component, this event is propagated to the component's parent node.
- Non-bubbling event: When an event is triggered on a component, this event is not propagated to the component's parent node.
List of events
Note: Unless otherwise specified, the custom events of components other than those listed above are non-bubbling events. For example, the
submitevent of<form />,inputevent of<input />, andscrollevent ofscroll-view. For more information, see Components.
Event binding
An event can be bound by using the following two types of syntax:
bind:{event name}indicates to bind an event.catch:{event name}indicates to bind an event and prevent the event from bubbling up.
Bind an event
Bind an event with bind:.
Bind an event and prevent bubbling
Bind an event with catch:.
For example:
-
Tap
inner viewto callhandleTap3andhandleTap2. Because thetapevent is propagated to the middle view, whereas the middle view prevents thetapevent from being propagated to the parent node. -
Tap
middle viewto triggerhandleTap2. -
Tap
outer viewto triggerhandleTap1.
Event object
Unless otherwise specified, when a component triggers an event, the handler bound with this event at the logic layer receives an event object.
List of BaseEvent object properties
List of CustomEvent object properties (inherited from BaseEvent)
List of TouchEvent object properties (inherited from BaseEvent)
type
The event type.
timeStamp
The number of milliseconds from the moment when the page is opened until the event is triggered.
target
The source component that triggers the event.
currentTarget
The current component that is bound with the event.
Note: You can refer to the above example for
targetandcurrentTarget. When users tapinner view, the event objectstargetandcurrentTargetreceived byhandleTap3are bothinner, but thetargetandcurrentTargetreceived byhandleTap2areinnerandmiddle, respectively.
dataset
You can add some custom data to the component node. This way, the custom node data can be obtained from the event, facilitating logical processing.
In the template, these custom data starts with data- and multiple words are joined with a hyphen -. In the code, a hyphenation is converted to camel case, and the uppercase characters are automatically converted to lowercase ones. Example:
data-element-typeis converted to event.currentTarget.dataset.elementType.data-elementTypeis converted to event.currentTarget.dataset.elementtype.
Example:
touches
touches is an array representing the touch points staying in the screen. Every element is a Touch object. touches in the canvas touch event is a CanvasTouch array.
Touch object
changedTouches
The data format of changedTouches is the same as that of touches. changedTouches indicates the touch points that have changes, such as touchstart (appearance of touch points), touchmove (location changes), and touchendor touchcancel (disappearance).
detail
The data that is carried by a custom event. For example, the submission event of a form component carries the user's input, and the error event of media carries the error message. For more information, see the event definitions in components.
The x and y in detail of the tap event are similar to pageX and pageY. x and y represent the distance from the top left corner of the document.
Example
Bind an event handler to the component
For example, bind:tap. When the user taps the component, the specified event handler will be found in the Page.
In the Page definition, write the event handler with the parameter event.
You can see that the event object from the log looks like this: