The "events" app fires events by passing them to listener functions.
When the "events" service is loaded, it will read events.js from the
app directories of every installed app, and track which listener functions
should react to which named events.
To fire an event (meaning have an object passed to all listeners set up to
react to that event name), use the fire method:
App.$events.fire('my_custom_event', { number: 4 });
The "events" service would then asynchronously pass the given object to all listener functions confiugred to listen for "my_custom_event".
The default "events" app configuration looks like this:
module.exports = {
fireEvents: true
};
If the fireEvents property is false, calls to $events.fire will be ignored.
You can copy the configuration file to config/events.js using the following
command:
node . publish --config events
A listener is a function that takes a single argument (which will be the data passed when the event is fired).
function myListener(eventData) {
// do something with my event
}
The function can by async, but it will not be awaited.
To register a listener function from one of your apps, export a map of event
names to arrays of listener functions from event.js in your app directory.
Example:
// apps/my_custom_app/events.js
module.exports = {
some_event_name: [
(data) => { console.log(data); }
]
};