Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Event Aggregator Use Cases

Ghislain B edited this page Jan 30, 2020 · 6 revisions

Use Case #1 (typical)

A typical use case is to use the Event Aggregator as a global pub/sub (publish/subscribe). A user would typically send (publish) data (which could be any type of data) with a key identifier which is used as a Singleton Service.

Example with Parent/Child communication

// Child (sending user form)
import { EventAggregator } from 'aurelia-event-aggregator';

@inject(EventAggregator)
export class HelloChild{

  constructor(private ea: EventAggregator) { }

  save(user) {
    this.ea.publish('form:saved', user);
  }
}
// Parent (listening to user form)
import { EventAggregator, Subscription } from 'aurelia-event-aggregator';

@inject(EventAggregator)
export class HelloParent {
  subscription: Subscription;

  constructor(private ea: EventAggregator) {
    this.subscription = this.ea.subscribe('form:saved', (user) => alert(`Hello ${user.firstName}`));
  }

  dispose() {
    // don't forget to dispose of the subscription to avoid any side effect
    this.subscription.dispose();
  }
}

Contents

Clone this wiki locally