Aurelia 退订事件聚合器
Aurelia Unsubscribe Event Aggregator
我正在使用 Aurelia Framework
和 Typescript
,在 event aggregator
中我可以发布和订阅频道。
问题是我无法取消订阅频道。
Note: All forms of the subscribe method return a dispose function. You can call this function to dispose of the subscription and discontinue receiving messages. A good place to dispose is either in a view-model's deactivate callback if it is managed by a router, or in its detached callback if it is any other view-model.
这是从 aurelia official documentation website 中摘录的,我似乎不太明白如何实现它。
我继续 aurelia gitter channel 并发现了 3 个关于此的讨论,其中一个给出了以下取消订阅的示例:
sub = ea.subscribe();
//unsubscribe the event
sub();
问题是此代码在 TypeScript 中不起作用。
如何取消订阅 Typescript 中的 event aggregator
?
现在,使用此代码
@inject(Publisher, Subscriber)
export class Home {
publisher: Publisher;
subscriber: Subscriber;
channelName = "testChannel";
constructor(pub: Publisher, sub: Subscriber) {
this.publisher = pub;
this.subscriber = sub;
this.subscriber.subscribe(this.channelName);
this.publisher.publish(this.channelName, "Ana are mere");
}
}
@inject(EventAggregator)
export class Publisher {
eventAggregator: EventAggregator = null;
constructor(agg: EventAggregator) {
this.eventAggregator = agg;
}
publish(channelName: string, object: Object) {
this.eventAggregator.publish(channelName, object);
}
}
@inject(EventAggregator)
export class Subscriber {
eventAggregator: EventAggregator = null;
constructor(agg: EventAggregator) {
this.eventAggregator = agg;
}
subscribe(channelName: string) {
this.eventAggregator.subscribe(channelName, object => {
//TODO do something with received object
alert(object);
});
}
unsubscribe(channelName: string) {
debugger;
}
}
执行Home
组件时,subscribe方法不是只执行一次,而是调用构造函数执行多次。所以,如果我已经在主页上 3 次,它将被执行 3 次。
所以:
为什么我的订户方法被多次触发?
如何在 TypeScript 中取消订阅 event-aggregatoor
?
谢谢!
您需要取消订阅 deactivate()/detach(),据我所知,使用 Typescript 不会改变这一点。
10/14/2015 编辑
EventAggregator class 的 subscribe
函数 returns 一个 "dispose" 函数 "subscription" 对象:
var subscription = eventAggregator.subscribe('some event', value => alert(value));
您需要保留对订阅对象的引用,以便在不再需要时销毁订阅。
在视图模型中,订阅事件的最佳时间是 attached
。同样,取消订阅的最佳时间是 detached
。
这是您的 Home
视图模型在使用此模式时的样子(注意:我删除了您的 Subscriber 和 Publisher classes 因为我认为它们增加了不必要的复杂性围绕 EventAggregator 并且很难解释您的问题的解决方案)。
@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();
}
}
我正在使用 Aurelia Framework
和 Typescript
,在 event aggregator
中我可以发布和订阅频道。
问题是我无法取消订阅频道。
Note: All forms of the subscribe method return a dispose function. You can call this function to dispose of the subscription and discontinue receiving messages. A good place to dispose is either in a view-model's deactivate callback if it is managed by a router, or in its detached callback if it is any other view-model.
这是从 aurelia official documentation website 中摘录的,我似乎不太明白如何实现它。
我继续 aurelia gitter channel 并发现了 3 个关于此的讨论,其中一个给出了以下取消订阅的示例:
sub = ea.subscribe();
//unsubscribe the event
sub();
问题是此代码在 TypeScript 中不起作用。
如何取消订阅 Typescript 中的 event aggregator
?
现在,使用此代码
@inject(Publisher, Subscriber)
export class Home {
publisher: Publisher;
subscriber: Subscriber;
channelName = "testChannel";
constructor(pub: Publisher, sub: Subscriber) {
this.publisher = pub;
this.subscriber = sub;
this.subscriber.subscribe(this.channelName);
this.publisher.publish(this.channelName, "Ana are mere");
}
}
@inject(EventAggregator)
export class Publisher {
eventAggregator: EventAggregator = null;
constructor(agg: EventAggregator) {
this.eventAggregator = agg;
}
publish(channelName: string, object: Object) {
this.eventAggregator.publish(channelName, object);
}
}
@inject(EventAggregator)
export class Subscriber {
eventAggregator: EventAggregator = null;
constructor(agg: EventAggregator) {
this.eventAggregator = agg;
}
subscribe(channelName: string) {
this.eventAggregator.subscribe(channelName, object => {
//TODO do something with received object
alert(object);
});
}
unsubscribe(channelName: string) {
debugger;
}
}
执行Home
组件时,subscribe方法不是只执行一次,而是调用构造函数执行多次。所以,如果我已经在主页上 3 次,它将被执行 3 次。
所以:
为什么我的订户方法被多次触发?
如何在 TypeScript 中取消订阅 event-aggregatoor
?
谢谢!
您需要取消订阅 deactivate()/detach(),据我所知,使用 Typescript 不会改变这一点。
10/14/2015 编辑
EventAggregator class 的 subscribe
函数 returns 一个 "dispose" 函数 "subscription" 对象:
var subscription = eventAggregator.subscribe('some event', value => alert(value));
您需要保留对订阅对象的引用,以便在不再需要时销毁订阅。
在视图模型中,订阅事件的最佳时间是 attached
。同样,取消订阅的最佳时间是 detached
。
这是您的 Home
视图模型在使用此模式时的样子(注意:我删除了您的 Subscriber 和 Publisher classes 因为我认为它们增加了不必要的复杂性围绕 EventAggregator 并且很难解释您的问题的解决方案)。
@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();
}
}