Plain class events: tp.On(), handleEvent() and EventTarget
This demo shows how the Tripous DOM event helpers can be used with plain JavaScript code.
The classes in this page do not inherit from tp.Object, tp.Component,
or any other Tripous class. They use the standard browser event model.
tp.On() is a small helper around addEventListener(). It accepts a
selector, an element, window, document, or any object that supports
addEventListener(). The listener may be a function, or it may be an object with a
handleEvent(event) method.
EventTarget is the browser base class for objects that can send and receive DOM-style
events. A plain class may extend EventTarget, use tp.On() for subscribers,
and fire events with tp.Trigger().
Object listener with handleEvent(event)
A plain object can be a DOM event listener when it has a
handleEvent(event) method. In this case the object itself is passed to
tp.On(). When the button is clicked, the browser calls
MyClass.handleEvent(event).
Listener function
The most common case is passing a function as the listener. The function receives the native browser event object.
Plain sender with EventTarget and tp.Trigger()
A plain JavaScript class can also send events. It extends EventTarget,
subscribers use tp.On(Sender, "plain-change", ...), and the sender fires
a CustomEvent with tp.Trigger().
Run all checks
This button uses tp.Trigger() to fire the two button clicks and then asks
the plain sender to fire its custom event.