I have this component called beat-player, on the PHP code I receive a dispatched event from another component and it works:
<?php
namespace AppLivewire;
use LivewireComponent;
use LivewireAttributesOn;
class BeatPlayer extends Component
{
public $scores;
public $difficulty;
public $note_type;
public $sample_urls ;
// public function mount($score)
// {
// $this->score = $score;
// }
public function mount()
{
$this->sample_urls = [
'snare' => asset('samples/snare.wav'),
'kick' => asset('samples/kick2.wav'),
'hat' => asset('samples/hihat2.wav')
];
}
#[On('score-updated')]
public function handleUpates($updates){
$this->scores = $updates['scores'];
$this->difficulty = $updates['difficulty'];
$this->note_type = $updates['note-type'];
}
public function updateScore($scores)
{
$this->scores = $scores;
}
public function updateDifficulty($difficulty){
$this->difficulty=$difficulty;
}
public function updateNoteType($note_type)
{
$this->note_type = $note_type;
}
public function render()
{
// dd($this);
return view('livewire.beat-player');
}
}
on the blade code I am trying to handle it as well so I can trigger some updates on a 3rd party library:
@script
<script>
......
snip
......
window.sequencer = a();
$wire.on('score-updated',(updates)=>{
console.log('updates')
});
</script>
@endscript
this event is not being triggered.
is it possible to handle in both parts?
in essence there is a property that gets updated ($scores) that I need to know when that happens.
thanks in advance
2
Answers
I think, if I understood your description correctly, the issue is that the
score-updated
event is dispatched from another component.Looking at the docs on this
https://livewire.laravel.com/docs/events#listening-for-events-inside-component-scripts
it says:
One way you can test this would be to dispatch a new event from your
handleUpates
function:Then listen for
pass-through-score-updated
in your script.If that works then you could leave it at that or look into dispatching events to other components:
https://livewire.laravel.com/docs/events#dispatching-directly-to-another-component
Hopefully that helps
This generic JS listener can work: