What is an event?

  • Events are the communication method from the presentation layer to the logical layer.
  • Events can give feedback of users’ actions to the logical layer for processing.
  • Events can be bound to components, and when the trigger condition is reached, the corresponding event function in the logical layer will be executed.
  • The event object can carry additional information, such as id, dataset, touche

How to use

Events include bubbling events and non-bubbling events:

  • Bubbling event: When an event on a component is triggered, the event will be delivered to the parent node.
  • Non-bubbling event: When an event on a component is triggered, the event will not be delivered to the parent node.

The writing method of event binding in the form of “key” and “value”is the same as the attribute of components

  • The key starts with “on” or “catch”, plus the type of events such as onTap, catchTap
  • “value” is a string, and a function with the same name needs to be defined in the corresponding “Page”. Otherwise, an error will be reported when the event is triggered.

The “on” event binding can not prevent the bubbling event from being passed up, while the “catch” event binding can do so.

  1. <view id="outter" onTap="handleTap1">
  2. view1
  3. <view id="middle" catchTap="handleTap2">
  4. view2
  5. <view id="inner" onTap="handleTap3">
  6. view3
  7. </view>
  8. </view>
  9. </view>

In the above code, clicking on “view3” will trigger “handleTap3” and “handleTap2” successively (The “tap” event will be passed to “view2”, and “view2” will prevent the “tap” event from bubbling which will no longer be passed to the parent node). Clicking on “view2” will trigger “handleTap2”, and clicking on “view1” will trigger “handleTap1”.

List of bubbling events:

Type Triggering condition
touchStart Touch starts
touchMove Move after touch
touchEnd Touch ends
touchcancel The touch action is interrupted, such as call reminder, pop-up window
tap Touch and leave instantly
longTap After touching, leave after 300 ms

Other events do not bubble up:

  • Bind an event handler function in the component.
    For example of “onTap” when users click on the component, the corresponding event handler will be found in the corresponding “Page” of the page.

    1. <view id="tapTest" data-hi="Alipay" onTap="tapName">
    2. <view id="tapTestInner" data-hi="AlipayInner">
    3. Click me!
    4. </view>
    5. </view>
  • Write the corresponding event processing function in the corresponding “Page” definition, and the parameter is “event”:

    1. Page({
    2. tapName(event) {
    3. console.log(event)
    4. }
    5. })
  • You can see that the information in the “log”is as follows:

    1. {
    2. "type": "tap",
    3. "timeStamp": 13245456,
    4. "target": {
    5. "id": "tapTestInner",
    6. "dataset": {
    7. "hi": "Alipay"
    8. },
    9. "targetDataset": {
    10. "hi": "AlipayInner"
    11. }
    12. },
    13. "currentTarget": {
    14. "id": "tapTest",
    15. "dataset": {
    16. "hi": "Alipay"
    17. }
    18. }
    19. }

    Event object

When the component triggers an event, the logical layer bound to the event handler will receive an event object

  • BaseEvent: A list of basic event object attributes. | Attribute | Type | Description | | —- | —- | —- | | type | String | Event type | | timeStamp | Integer | Timestamp when the event was generated | | target | Object | The collection of attribute values of the componenttriggering the event |
  • CustomEvent:Attribute list of custom event object (extending BaseEvent). | Attribute | Type | Description | | —- | —- | —- | | detail | Object | Additional information |
  • TouchEvent:Attribute list of custom event object (inherited from BaseEvent). | Attribute | Type | Description | | —- | —- | —- | | touches | Array | Array of touch point information currently staying on the screen | | changedTouches | Array | Current changing array of touch point information |
  • Type: The type of event.

  • timeStamp:The number of milliseconds that have elapsed since the page was opened to the trigger event.

  • target:The source component triggering the event. | Attribute | Type | Description | | —- | —- | —- | | id | String | ID of the event source component | | tagName | String | The type of the current component | | dataset | Object | A collection of custom attributes starting with “data-” on the component to which the event is bound | | targetDataset | Object | The component that actually triggers the event is determined by
    a collection of custom attributes starting with “data-” |

dataset

Data can be defined in the component, and these data will be passed to the logical layer through events.

Writing style: It starts with “data-” with multiple words linked by a hyphen (-) without uppercase letter(Uppercase letters will be automatically converted into lowercase letter. For example,“data-element-type” will eventually converthyphen to camel case “elementType” in the “dataset”.

Code example:

  1. <view data-alpha-beta="1" data-alphaBeta="2" onTap="bindViewTap"> DataSet Test </view>
  1. Page({
  2. bindViewTap:function(event){
  3. event.target.dataset.alphaBeta === 1 // - 会转为驼峰写法
  4. event.target.dataset.alphabeta === 2 // 大写会转为小写
  5. }
  6. })

touches

“touches” is an array with each element a “Touch” object (the “touches” carried in the “canvas” touch event is an array of “CanvasTouch”), which represents the touch point currently staying on the screen.

  • Touch object | Attribute | Type | Description | | —- | —- | —- | | identifier | Number | Identifier of the touch point | | pageX, pageY | Number | The distance ranges from the upper left corner of the document. the upper left corner is the origin, the horizontal axis is the X axis, and the vertical axis is the Y axis | | clientX, clientY | Number | The distance ranges from the upper left of the displayable area on the screen (excluding the navigation bar on the screen). The horizontal axis is the X axis, and the vertical axis is the Y axis. |
  • CanvasTouch Object | Attribute | Type | Description | | —- | —- | —- | | identifier | Number | Identifier of the touch point | | x, y | Number | The distance ranges from the upper left corner of the document. the upper left corner is the origin, the horizontal axis is the X axis, and the vertical axis is the Y axis |
  • changedTouches: The data format of “changedTouches” is the same as “touches”,indicating a changing touch point , such as change from invariance (touchstart), position change (touchmove), and change from presence (touchend, touchcancel).

  • detail: The data are carried by the custom event. For example, the submission event of the form component will carry the user’s input information, and the media error event will carry the error information. For detailed description, please refer to the definition of each event in the component definition.