Aurelia-event-aggregator
提供:Dev Guides
Aurelia-イベントアグリゲーター
イベントをより多くのリスナーにアタッチする必要がある場合、またはアプリの機能を観察してデータの更新を待つ必要がある場合は、イベントアグリゲーターを使用する必要があります。
Aureliaイベントアグリゲーターには3つの方法があります。 publish メソッドはイベントを発生させ、複数のサブスクライバーが使用できます。 イベントをサブスクライブするには、 subscribe メソッドを使用できます。 最後に、 dispose メソッドを使用して、サブスクライバーをデタッチできます。 次の例はこれを示しています。
ビューには、3つの機能ごとに3つのボタンがあります。
appl
<template>
<button click.delegate = "publish()">PUBLISH</button><br/>
<button click.delegate = "subscribe()">SUBSCRIBE</button><br/>
<button click.delegate = "dispose()">DISPOSE</button>
</template>
*eventAggregator* をインポートして、使用する前に挿入する必要があります。
app.js
import {inject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
@inject(EventAggregator)
export class App {
constructor(eventAggregator) {
this.eventAggregator = eventAggregator;
}
publish() {
var payload = 'This is some data...';
this.eventAggregator.publish('myEventName', payload);
}
subscribe() {
this.subscriber = this.eventAggregator.subscribe('myEventName', payload => {
console.log(payload);
});
}
dispose() {
this.subscriber.dispose();
console.log('Disposed!!!');
}
}
*SUBSCRIBE* ボタンをクリックして、将来公開されるデータをリッスンする必要があります。 加入者が接続されると、新しいデータが送信されるたびに、コンソールはそれを記録します。 *PUBLISH* ボタンを5回クリックすると、毎回ログに記録されることがわかります。
*DISPOSE* ボタンをクリックして、サブスクライバーをデタッチすることもできます。