How to realize ajax .done in livewire

Viewed 516

I want to refresh some html in other container, not where the button is.

In native ajax is like this

.done(function(result){
                     $('#result').html(result); 
                  });

how it works in livewire alpinejs?

1 Answers

you have 2 option

  1. use Livewire Life Cycle Javascript Hooks you can see the livewire javascript hooks in this link Lifecycle Hooks

    document.addEventListener("DOMContentLoaded", () => {
    Livewire.hook('message.processed', (message, component) => {})
    

    });

  2. Use Browser Events Dispatching

    Dispatching Browser Events you can dispatch a browser event when ever you want in you backend like this

$this->dispatchBrowserEvent('name-updated', ['newName' => $value]);

and for js side you can use this code:

window.addEventListener('name-updated', event => {
    alert('Name updated to: ' + event.detail.newName);
})
Related