In this episode, we're taking a closer look at enhancing our delete functionality by adding a native JavaScript confirmation before actually deleting an item. First, we notice that our current Livewire setup uses the wire:click
directive in the blade template, but as soon as we want to add a JavaScript confirmation, this approach needs to change.
We start by commenting out the old wire:click
method and then convert our delete button into an Alpine component. If you're not super familiar with Alpine.js, it's this lightweight JavaScript framework that works really closely with Livewire and is already set up for us.
The episode walks you through wiring up an x-on:click
event in Alpine to display a simple window.confirm()
dialog. If the user confirms, that's our cue to actually perform the delete; otherwise, nothing happens.
Next, we explore how to call Livewire actions directly from our Alpine/JavaScript handler. Turns out, Livewire exposes a handy JavaScript API (the @this
or $wire
property in Alpine, depending on your version) that lets you do exactly what you would with the wire:click
, but from JavaScript instead. We use this to call the parent.deleteBook()
Livewire method, passing in the book ID.
By the end of the video, we've got a really flexible setup: we can confirm deletions with any custom JS logic or even a third-party modal, and then trigger Livewire methods from our scripts. Super useful for any scenario where you want to mix more complex client-side stuff with your Livewire components.