Event Observer (4h)
An event has two parts. In the core, we dispatch
(or fire
) an event to create customize point. In the plugin, we subscribe
(or listen
) to execute our custom code.
Fire an event
import {fire} from "../src/event-bus";
...
// Fire an event to allow modify logic
fire('[name_of_event]', [event_data]);
Listen an event
import {listen} from "../src/event-bus";
...
// Listen an event
listen('[name_of_event]', (eventData) => {
// Custom logic here
}, [listener_tag]);
name_of_event: must be same as
fire
name_of_eventobserver: is a function that receive
eventData
parameter fromfire
eventlistener_tag: to identify listener. Listeners that registed with same tag is overrided. Only last observer with same tag (and same name_of_event) is active.
Event naming
To manage events easier, we should naming events following this format [object_type]_[object_name]_[method/action]_[event_position]
For example, an event name model_customer_save_after
means 1. object_type: model
2. object_name: customer
3. method/action: save
4. event_position: after
Recommended Reading
Exercise: Use event/observer, after add product to cart, show successfully message , quantity of product added