Skip to content

Events

Experimental
View source

Events are a mechanism supporting decoupled communication between components. They are part of the underlying API abstract class meaning they are available to all child classes. The usage for Events is simple and familiar.

On a given service, you can subscribe to a topic using the on() method, or you can fire an event using trigger().

The best example of this is the usage as part of the authentication flow.

In a number of the methods within the Auth API, the trigger() method is called.

this.trigger('auth_updated', {
method: 'switch_context',
data: {
user: body.data.user,
business: body.data.business,
refresh_token: body.data.tokens.refresh,
access_token: body.data.tokens.access,
}
});

Auth.switchContexts

Subscribing to the auth_updated topic is also as straightforward as this.

Xedi.Auth.on('auth_updated', (event) => {
console.log(`Auth state updated by ${ event.model }`);
console.log(`Current user: ${ event.data.user.first_name });
if (event.data.hasOwnProperty('business')) {
console.log(`Current business: ${ event.data.business.name }`);
}
});
These are not DOMEvents, nor do they implement the JavaScript Event interface.
Edit this page on GitHub
1 contributorSmudge3806
Last edited by Smudge3806 on June 22, 2020