left-icon

Aurelia Succinctly®
by Matthew Duffield

Previous
Chapter

of
A
A
A

CHAPTER 12

Event Aggregation

Event Aggregation


We have looked at services and how they can come in handy, but what about times when we need to communicate events across classes? It would be really cool if Aurelia supported some form of publish/subscribe model in the framework. Well, look no further—that is exactly what the aurelia-event-aggregator does.

Consider the use case where you present a user with a login screen, and upon a successful login, you want to build up their dashboard and menu tree. This is all possible using the event aggregator. Let’s take a look at an example:

Code Listing 113

imports {EventAggregator} from 'aurelia-framework';

imports {DataService} from './services/data-service';

export class Login {

 static inject = [EventAggregator, DataService];

 username = '';

 password = '';

 constructor(eventAggregator, dataService) {

 this.eventAggregator = eventAggregator;

 this.dataService = dataService;

 }

 async performLogin() {

 let response = await this.dataService.authenticate(

  this.username, this.password);

 if (response.success) {

  this.loginSuccess(response.user);

 }

 loginSuccess(user) {

 this.eventAggregator.publish('login-success', user);

 }

}

We have our Login class, and we first call our performLogin function, which tries to authenticate our user by passing in username and password. It returns a response object that has a Boolean success property as well as a user property, if successful. If the user passes authentication, then we call loginSuccess, passing in the user object. The event aggregator publishes a channel with the name login-success and the payload of the user object.

It is important to have a good handle on your channels that you use with the event aggregator. A common approach might be to create a ChannelService that simply has all of the channels that your application uses for communication over the event aggregator. This will help mitigate any potential problems with misspellings, etc. It is also possible to use types in the form of a class to represent the channel.

Let’s take a look at how we might subscribe to this event. Consider the following:

Code Listing 114

imports {EventAggregator} from 'aurelia-framework';

imports {DataService} from './services/data-service';

export class Welcome {

 static inject = [EventAggregator, DataService];

 dashboard = [];

 constructor(eventAggregator, dataService) {

 this.eventAggregator = eventAggregator;

 this.dataService = dataService;

 this.eventAggregator.subscribe('login-success',

  this.onLoginSuccess);

 }

 onLoginSuccess(user) {

 this.dashboard = await this.dataService.getDashboardByUserId(

  user.id);

 }

}

Here we are subscribing to the login-success channel. We pass the onLoginSuccess callback, and it in turn performs a lookup to get dashboard information based on the user ID. The event aggregator gives us the ability to provide rich communication across our classes, and yet still preserve separation of concerns.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.