Class “Listeners”

Object > Listeners

Objects of the type Listeners provide methods to manage listeners and trigger events. They encapsulate the tabris event system in a way that is more convenient to use with TypeScript and the ES2017 async/await feature.

Type: Listeners<EventData> extends Object
Generics: EventData: A plain object containing the data to be passed to the listeners in addition to target, type and timeStamp. Must be a subclass of {target: object} and defaults to undefined.
Constructor: public
Singleton: No
Namespace: tabris
Direct subclasses: None
JSX Support: No

Constructor

new Listeners(target, type)

Parameter Type Description
target object  
type string  

Methods

addListener(listener)

Registers a listener to be notified by new events. Each listener can only be added once. Returns the target object. Instances of Listeners can also be called directly as a function to register a new listener. I.e. widget.onResize(listener); is the same as widget.onResize.addListener(listener); It is also the same as widget.on('resize', listener');, but with better TypeScript support.

Parameter Type Description
listener Function The listener function called with the event object

Returns: this.target

once(listener)

Notifies the given listener the next time an event is issued, but not afterwards. Returns the target object.

Parameter Type Description
listener Function The listener function called with the event object

Returns: this.target

promise()

Returns a promise that resolves the next time an event is issued. The dispatched event object will be used as the resolved value.

Returns: Promise<EventObject>

removeListener(listener)

Deregisters a listener, it will not be notified of future events. Returns the target object.

Parameter Type Description
listener Function  

Returns: this.target

trigger()

Issues a plain event object to all registered listeners with a newly constructed event object.

Returns: this.target

trigger(eventData?)

Issues an event object to all registered listeners. If an uninitialized (not previously issued) instance of EventObject is given as the argument it will be issued directly as-is. Any other type of object (including an already initialized/previously issued event object) will be copied to create a new event object. This allows for simple event re-routing.

Parameter Type Description
eventData EventData Optional.

Returns: this.target

triggerAsync(eventData?)

Like trigger, but returns a promise. The promise will resolve when all asynchronous listeners (if any) have resolved. If none of the listeners are asynchronous (return a promise) this method works just like trigger(). Useful for unit testing.

Parameter Type Description
eventData object Optional.

Returns: Promise</code>

Properties

target

The object that issues the events, e.g. a widget.

Type: object
Settable: No

type

The event type. This value will be set in the type field of the event object given to the listener. It is the same value used by the on and trigger methods.

Type: string
Settable: No