- Aurelia - Best Practices
- Aurelia - Community
- Aurelia - Debugging
- Aurelia - Bundling
- Aurelia - Tools
- Aurelia - Localization
- Aurelia - Dialog
- Aurelia - Animations
- Aurelia - History
- Aurelia - Routing
- Aurelia - Refs
- Aurelia - HTTP
- Aurelia - Forms
- Aurelia - Event Aggregator
- Aurelia - Events
- Aurelia - Converters
- Aurelia - Binding Behavior
- Aurelia - Data Binding
- Aurelia - Plugins
- Aurelia - Configuration
- Aurelia - Dependency Injections
- Aurelia - Custom Elements
- Aurelia - Component Lifecycle
- Aurelia - Components
- Aurelia - First Application
- Aurelia - Environment Setup
- Aurelia - Overview
- Aurelia - Home
Aurelia Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Aurepa - Event Aggregator
Event aggregator should be used when your events need to be attached to more psteners or when you need to observe some functionapty of your app and wait for the data update.
Aurepa event aggregator has three methods. The pubpsh method will fire off events and can be used by multiple subscribers. For subscribing to an event, we can use the subscribe method. And finally, we can use the dispose method to detach the subscribers. The following example demonstrates this.
Our view will just have three buttons for each of the three functionapties.
app.html
<template> <button cpck.delegate = "pubpsh()">PUBLISH</button><br/> <button cpck.delegate = "subscribe()">SUBSCRIBE</button><br/> <button cpck.delegate = "dispose()">DISPOSE</button> </template>
We need to import eventAggregator and inject it before we are able to use it.
app.js
import {inject} from aurepa-framework ; import {EventAggregator} from aurepa-event-aggregator ; @inject(EventAggregator) export class App { constructor(eventAggregator) { this.eventAggregator = eventAggregator; } pubpsh() { var payload = This is some data... ; this.eventAggregator.pubpsh( myEventName , payload); } subscribe() { this.subscriber = this.eventAggregator.subscribe( myEventName , payload => { console.log(payload); }); } dispose() { this.subscriber.dispose(); console.log( Disposed!!! ); } }
We need to cpck the SUBSCRIBE button to psten for data that will be pubpshed in future. Once the subscriber is attached, whenever new data is sent, the console will log it. If we cpck the PUBLISH button five times, we will see that it is logged every time.
We can also detach our subscriber by cpcking the DISPOSE button.
Advertisements