F
Filament16mo ago
TiBiBa

Possible to dispatch event in submit() function after variables are updated?

Hi! When updating from v2 to v3 we ran into an error that we are unable to solve. Within a custom page with a form we perform some data mutations in the submit() function and store these in a variable $this->tree. Which, when exists, is automatically updated in the rendered page. Lastly in the function we threw an emit() to trigger a JS function in the front-end and load an external library. This al worked fine but with the switch to v3 (and Livewire v3) emit() is replaced by dispatch(). For some reason the ordering of events is now changed. The dispatch() is thrown before the variable is updated on the front-end, making the JS library crash as no value is found. The same use-case worked fine in v2. Is there a way to force a re-render within the submit() function before I manually dispatch an event? Alternatively would be to dispatch the custom event after the updating in some way. Thanks in advance!
Solution:
Yeah, I think this is related to Livewire's event batching (events are grouped together in v3 for better performance). A possible workaround is to delay the event handler on the front-end:
x-on:my-event.window="$nextTick(() => /* handle event here */)"
x-on:my-event.window="$nextTick(() => /* handle event here */)"
...
Jump to solution
2 Replies
Solution
Patrick Boivin
Patrick Boivin16mo ago
Yeah, I think this is related to Livewire's event batching (events are grouped together in v3 for better performance). A possible workaround is to delay the event handler on the front-end:
x-on:my-event.window="$nextTick(() => /* handle event here */)"
x-on:my-event.window="$nextTick(() => /* handle event here */)"
TiBiBa
TiBiBaOP16mo ago
This works, you absolutely made my day. Was working on a solution for hours. Thanks!
Want results from more Discord servers?
Add your server