F
Filament17mo ago
sm

Integrating with Livewire/Laravel eventing.

Hey guys, I was wondering if anyone could point me to docs/source on how filament integrates with livewire eventing? For example, if I have a TableWidget that's included via a ViewComponent and fire an event from an action within the TableWidget (for example: a select action), I can't seem to listen for that event from the parent Form. I've tried just adding a protected $listeners = ['eventName' => 'someFunc']; and setting $this->data from there, but the event doesn't seem to get through (is this something to do with emit direction?). Also, it doesn't seem like I can use $this->emit() on form component closures, even though Filament uses livewire under the hood, is that expected? What's the right way to dispatch an event? is it using dispatchEvent from a closure to which we pass the current instance? Thanks!
5 Replies
sm
smOP17mo ago
I'd also like to be able to call $this->refreshFormData from my event handler, just wanted to make sure that's supported as well?
LeandroFerreira
LeandroFerreira17mo ago
you need to share some code if you are using lv3, you should do
use Livewire\Attributes\On;

#[On('event-name')]
public function someFunc()
{
//...
}
use Livewire\Attributes\On;

#[On('event-name')]
public function someFunc()
{
//...
}
$this->dispatch('event-name')
$this->dispatch('event-name')
awcodes
awcodes17mo ago
I’d recommend reading through the LW3 upgrade guide. Emit no longer exists. Everything is either dispatch() or dispatchTo() now.
sm
smOP17mo ago
Follow up question: What does registerListeners do on a field/Component? Sorry, to add additional context: When should one prefer to use registerListeners directly on a field, vs using On a func in the Component? It looks like the only component that uses registerListeners is Wizard.
awcodes
awcodes17mo ago
It does what it sounds like it does. It registers event listeners that can be run when the event is dispatched from somewhere in the form component. Because components themselves aren’t livewire components. However, this is not needed as much anymore since actions have been split out. Now you can register an action instead. Have a look at the Repeater in v2 vs the Repeater in v3. That might help to make the concept for clear.

Did you find this page helpful?