If you include the aurelia-event-aggregator
plugin using "basicConfiguration
" or "standardConfiguration
" then the singleton EventAggregator's API
will be also present on the Aurelia
object. You can also create additional instances of the EventAggregator
, if needed, and "merge
" them into any object. To do this, import includeEventsIn
and invoke it with the object you wish to turn into an event aggregator
. For example includeEventsIn(myObject)
. Now my object has publish
and subscribe
methods and can be used in the same way as the global event aggregator
, detailed below.
Publishing on a Channel : TypeScript
import {autoinject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
@autoinject
export class APublisher {
constructor(private eventAggregator: EventAggregator) { }
publish(): void {
var payload = {};
this.eventAggregator.publish('channel name here', payload);
}
}
Subscribing to a Channel : TypeScript
import {autoinject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
@autoinject
export class ASubscriber {
constructor(private eventAggregator: EventAggregator) { }
subscribe(): void {
this.eventAggregator.subscribe('channel name here', payload => {
...
});
}
}
Publishing a Message : TypeScript
//some-message.ts
export class SomeMessage{ }
//a-publisher.ts
import {autoinject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
import {SomeMessage} from './some-message';
@autoinject
export class APublisher {
constructor(private eventAggregator: EventAggregator) { }
publish(): void {
this.eventAggregator.publish(new SomeMessage());
}
}
Subscribing to a Message Type : TypeScript
import {autoinject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
import {SomeMessage} from './some-message';
@autoinject
export class ASubscriber {
constructor(private eventAggregator: EventAggregator) { }
subscribe(): void {
this.eventAggregator.subscribe(SomeMessage, message => {
...
});
}
}