Yoyo provides a robust event system for communication between components on the same page. Events can be emitted from component methods or templates and listened to by other components or JavaScript.
Emitting Events (PHP)
emit('eventName', ...$args): Emits to all Yoyo components.emitUp('eventName', ...$args): Emits to parent components only.emitTo('componentName', 'eventName', ...$args): Emits to a specific component by name or CSS selector (selectors do not support arguments).emitSelf('eventName', ...$args): Emits to the current component.
Listening for Events (PHP)
Register listeners in a component using the protected $listeners property. The key is the event name, and the value is the method name.
Listening for Events (JavaScript)
Use Yoyo.on('eventName', callback) to listen for component-emitted events in the browser.
Dispatching Events (JavaScript)
Use Yoyo.dispatch('eventName', data) to trigger events globally, or Yoyo.dispatchTo('componentName', 'eventName', data) for specific components.
// Emitting from a method
$this->emit('counter-updated', $count);
// Listening in a component
protected $listeners = ['counter-updated' => 'showNewCount'];
protected function showNewCount($count)
{
$this->message = "The new count is: $count";
}
// Listening in JS
Yoyo.on('productAddedToCart', id => {
alert('ID: ' + id);
});
// Dispatching from JS
Yoyo.dispatch('post-created', { postId: 2 });