Aurelia Unsubscribe Event Aggregator Aurelia Unsubscribe Event Aggregator typescript typescript

Aurelia Unsubscribe Event Aggregator


10/14/2015 EDIT

The EventAggregator class's subscribe function returns a "dispose" function "subscription" object:

var subscription = eventAggregator.subscribe('some event', value => alert(value));

You need to keep a reference to the subscription object so that you can destroy the subscription when it's no longer needed.

In a view-model, the perfect time to subscribe to an event is when it's attached. Likewise, a perfect time to unsubscribe is when it's detached.

Here's what your Home view-model would look like if it used this pattern (note: I've dropped your Subscriber and Publisher classes because I think they're adding unnecessary complexity around the EventAggregator and are making it hard to explain the solution to your issue).

@inject(EventAggregator)export class Home {  eventAggregator: EventAggregator;  subscription: { dispose: () => void };  constructor(eventAggregator: EventAggregator) {    this.eventAggregator = eventAggregator;  }  attached() {    this.subscription = this.eventAggregator.subscribe('some event', value => alert(value));  }  detached() {    this.subscription.dispose();  }}


You need to unsubscribe on deactivate()/detach(), as far as I'm aware using Typescript doesn't change this.