Overview
Essentials
- Installation
- Introduction
- Defining a Component
- Property Decorators
- Manipulating DOM
Reusability
- Composition
- Custom Decorators
Architecture
- Event Handling
- State Management
Internals
- Component Lifecycle
- Initialisation Process
Tooling
- Debugging
- Deployment
- CLI
Miscellaneous
- Migration from Strudel 0.x
- Style Guide
Event Handling
Cross-component communication
As components are not aware of each other, they can be used in various combinations in various context. One of the ways of achieving the communication is by using Events. Strudel implements a simple class responsible for controlling event communication - EventEmitter
.
Using EventEmitter in components
EventEmitter
is available to be imported from Strudel API. It’s automatically included as a mixin for every component, which means by default every component is able to use event communication.
Key methods responsible for handling communication are:
$emit()
- activates all the listeners attached to the event with provided name$on()
- attaches listener to particular event$off()
- detaches listener to particular event
Full specification can be found in the API.
Example below shows cross-component event communication.
<div class="subscriber"></div> |
@Component('.subscriber') |
@Component('.publisher') |
Using EventEmitter outside of components
By extending the EventEmitter
you can provide the same publish-subscribe communication mechanism to any JavaScript class. The API is the same as in components.
import { EventEmitter } from 'strudel'; |