Events are the most commonly used behavior management methods in development.
Bind the processing of events to the component’s methods with the on- prefix。
Hint
: In San, both the DOM event and the component’s custom event are bound by the on- prefix, with no syntax distinction.
DOM Event
on- + event name binds the event of the DOM element to the component method. When a DOM event fires, the component method is called and this points to the component instance.
In the following example, when the button is clicked, the component’s submit method is called.
1 | san.defineComponent({ |
When binding events, you can specify parameters that reference the data in the current rendering environment. The argument can be any type of [expression] (../template/#expression).
1 | <!-- Template --> |
1 | // Component |
When specifying a parameter, $event is a special variable reserved by San, specifying that $event will be referenced to the DOM Event object.
So you can get event information such as the event-triggered DOM object, mouse position of the mouse event.
1 | san.defineComponent({ |
customized event
Custom events for components can be bound by the on- prefix on the component.
In the following example, MyComponent binds the done event of Label component with an event handler.
1 | var MyComponent = san.defineComponent({ |
San’s component architecture provides event functionality, and Label can easily dispatch an event directly by calling the fire method.
1 | var Label = san.defineComponent({ |
Modifier
capture
Version
:>= 3.3.0
The capture modifier is used in the element’s event declaration and the event is bound to the capture phase.
1 | var MyComponent = san.defineComponent({ |
Note
: This feature is only supported in browser environments that support addEventListener. Using the capture modifier on older IEs will have no effect.
native
Version
:>= 3.3.0
Using the native modifier in the component’s event declaration, the event is bound to the DOM event of the component’s root element.
1 | var Button = san.defineComponent({ |
Sometimes components encapsulate some infrastructure and styles, and hope that DOM events such as clicks and touches are handled by external consumers.
If the component needs fire each root element DOM event is cumbersome and difficult to maintain. The native modifier solves this problem.