javascript - livewire dispatchBrowserEvent is not triggering alert - Stack Overflow

I have a livewire ponent which has an update() function to update the ponent when it changes. The updat

I have a livewire ponent which has an update() function to update the ponent when it changes. The update() function is called and updates the item in the database as required. At the end of this function i run a dispatchBrowserEvent() to confirm the update but the corresponding function in my js file is not being called and thus the alert is not being triggered.

I have been using this method from the docs .x/events#browser

The livewire ponent:

<?php

namespace App\Http\Livewire;

use App\Models\Item;
use Livewire\Component;

class EditItem extends Component
{
    public int $item;
    public string $date = '';

    public function render()
    {
        return view('livewire.edit-item');
    }

    public function update()
    {

        $item             = Item::find( 1 );
        $item->date       = now();

        $item->save();

        $this->dispatchBrowserEvent('update-item', ['item' => $item->id, 'date' => $item->date]);

    }
}

its blade file:

<div>
    <button wire:click="update">Update Me!</button>
</div>

in my app.js I have the following:

jQuery(document).ready( function($) {
    /** LIVEWIRE EVENT ACTIONS **/
    window.addEventListener('update-item', event => {
        alert( 'Date updated to: ' + event.detail.date + 'on item with id of ' + event.detail.item );
    })

});

I have a livewire ponent which has an update() function to update the ponent when it changes. The update() function is called and updates the item in the database as required. At the end of this function i run a dispatchBrowserEvent() to confirm the update but the corresponding function in my js file is not being called and thus the alert is not being triggered.

I have been using this method from the docs https://laravel-livewire./docs/2.x/events#browser

The livewire ponent:

<?php

namespace App\Http\Livewire;

use App\Models\Item;
use Livewire\Component;

class EditItem extends Component
{
    public int $item;
    public string $date = '';

    public function render()
    {
        return view('livewire.edit-item');
    }

    public function update()
    {

        $item             = Item::find( 1 );
        $item->date       = now();

        $item->save();

        $this->dispatchBrowserEvent('update-item', ['item' => $item->id, 'date' => $item->date]);

    }
}

its blade file:

<div>
    <button wire:click="update">Update Me!</button>
</div>

in my app.js I have the following:

jQuery(document).ready( function($) {
    /** LIVEWIRE EVENT ACTIONS **/
    window.addEventListener('update-item', event => {
        alert( 'Date updated to: ' + event.detail.date + 'on item with id of ' + event.detail.item );
    })

});
Share Improve this question edited May 20, 2022 at 11:55 ThurstonLevi asked May 20, 2022 at 11:34 ThurstonLeviThurstonLevi 8592 gold badges21 silver badges46 bronze badges 2
  • you should change name function update to another name example edit , because has update function already in livewire. – Abdulmajeed Commented May 20, 2022 at 12:26
  • Not sure thats right unfortunately... the event is triggered in php and the database is updated (I have also confirmed using xdebug and a breakpoint in the function). Just that the javascript isn't fired by the dispatchBrowserEvent. the event is also fired but it doesn't fire the event in the app.js file. – ThurstonLevi Commented May 20, 2022 at 13:07
Add a ment  | 

2 Answers 2

Reset to default 2

remove this code in app.js

jQuery(document).ready( function($) {
    /** LIVEWIRE EVENT ACTIONS **/
    window.addEventListener('update-item', event => {
        alert( 'Date updated to: ' + event.detail.date + 'on item with id of ' + event.detail.item );
    })

});

in blade

<div>
        <button wire:click="update">Update Me!</button>
 
    <script>
        document.addEventListener('update-item', event => {
           alert( 'Date updated to: ' + event.detail.date + 'on item with id of ' + event.detail.item );
        })
    </script>
</div>

So it turned out i hadn't ran npm run dev so the javascript didn't exist. after running this all was good.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745195653a4616074.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信