Event aggregator
The Event Aggregator pattern in Aurelia allows disparate components and services to communicate through events without requiring direct references to each other. Reducing tight coupling promotes a cleaner and more maintainable codebase.
Configuration
Including Event Aggregator in Your Project
To use the Event Aggregator in your Aurelia application, include the aurelia-event-aggregator
plugin during the configuration phase. You can do this using either basicConfiguration
or standardConfiguration
.
Basic Configuration: Provides the essential Event Aggregator functionality.
Standard Configuration: Includes the Event Aggregator along with other commonly used utilities.
Accessing the Global Event Aggregator
Once configured, the singleton EventAggregator
is accessible via the Aurelia global object. This allows you to publish and subscribe to events throughout your application without manually injecting the Event Aggregator each time.
Creating Additional Event Aggregator Instances
In addition to the global Event Aggregator, you can create multiple instances as needed. This is useful for scoping events to specific parts of your application.
Using includeEventsIn
includeEventsIn
To add Event Aggregator capabilities to any object, use the includeEventsIn
function. This will extend the target object with publish
and subscribe
methods.
Publishing Events
Publishing events allow components to broadcast messages that other application parts can listen to.
Publishing to a Channel
You can publish events to named channels. Subscribers listening to these channels will receive the published payload.
Publishing a Message Object
Instead of using string channels, you can publish instances of message classes. This approach provides type safety and clarity.
Subscribing to Events
Subscribing to events allows components to react to messages published on specific channels or message types.
Subscribing to a Channel
Listen for events published to a specific channel by providing the channel name and a callback function.
Subscribing to a Message Type
Subscribe to specific message types by providing the message class and a callback function. This method leverages TypeScript's type system for enhanced reliability.
Merging Event Aggregator into Objects
To integrate Event Aggregator functionality into any custom object, use the includeEventsIn
method. This approach enables objects to publish and subscribe to events without directly interfacing with the Event Aggregator service.
Best Practices
Cleanup after yourself: Always unsubscribe from events when they are no longer needed to prevent memory leaks. This is usually done inside of the detached
lifecycle callback.
Use Message Classes for Clarity: Utilizing message objects instead of string channels can improve code maintainability and type safety.
Limit Global Event Usage: While the global Event Aggregator is convenient, consider creating scoped instances for more controlled event handling.
Last updated
Was this helpful?