کریم کیمیائی فر
7 ماه پیش توسط کریم کیمیائی فر مطرح شد
6 پاسخ

جایگزینی emit با dispatch در Livewire 3

سلام . من درحال راه اندازی sweetalert2 برای پنل ادمین پروژه ام هستم اما چونکه از اخرین نسخه لاراول و لایووایر استفاده میکنم و خیلی چیز ها تغییر کرده ، مشکلی در متد dispatchدارم.

یک addEventListener در app.js

import './bootstrap';
import Swal from 'sweetalert2';

const Toast = Swal.mixin({
    toast: true,
    position: 'bottom',
    showConfirmButton: false,
    timer: 7000,
    timerProgressBar: true,
    didOpen: (toast) => {
        toast.addEventListener('mouseenter', Swal.stopTimer)
        toast.addEventListener('mouseleave', Swal.resumeTimer)
    }
})

document.addEventListener('livewire:load', () => { // Here
    Livewire.on('toast', (type, message) => {
        Toast.fire({icon: type, title: message})
    })
})

script.blade.php

<!-- Must needed plugins to the run this Template -->
<script src="{{ asset('/Admin/js/jquery.min.js') }}"></script>
<script src="{{ asset('/Admin/js/popper.min.js') }}"></script>
<script src="{{ asset('/Admin/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('/Admin/js/bundle.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/date-time.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/setting.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/fullscreen.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/bootstrap-growl.js') }}"></script>

<!-- Active JS -->
<script src="{{ asset('/Admin/js/default-assets/active.js') }}"></script>

<!-- These plugins only need for the run this page -->
<script src="{{ asset('/Admin/js/default-assets/peity.min.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/peity-demo.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/gauge.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/serial.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/light.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/ammap.min.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/worldlow.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/radar.js') }}"></script>
<script src="{{ asset('/Admin/js/default-assets/dashboard-2.js') }}"></script>
@vite('resources/js/app.js') {{-- Here --}}
@livewireScripts

Label.php (livewire Component)

<?php

namespace App\Livewire\Admin\Settings\Footer;

use Illuminate\Support\Facades\DB;
use Livewire\Component;

class Label extends Component
{
    public $upLabel, $widgetLabel1, $widgetLabel2, $widgetLabel3, $rssLabel, $socialLabel,
        $supportLabel, $phoneLabel, $emailLabel, $aboutHeadLabel, $aboutBodyLabel, $copyrightLabel;

    public function render()
    {
        $footer = DB::connection('mysql-settings')->table('footers')->first();

        /* Variable */
        $this->upLabel        = $footer->upLabel;
        $this->widgetLabel1   = $footer->widgetLabel1;
        $this->widgetLabel2   = $footer->widgetLabel2;
        $this->widgetLabel3   = $footer->widgetLabel3;
        $this->rssLabel       = $footer->rssLabel;
        $this->socialLabel    = $footer->socialLabel;
        $this->supportLabel   = $footer->supportLabel;
        $this->phoneLabel     = $footer->phoneLabel;
        $this->emailLabel     = $footer->emailLabel;
        $this->aboutHeadLabel = $footer->aboutHeadLabel;
        $this->aboutBodyLabel = $footer->aboutBodyLabel;
        $this->copyrightLabel = $footer->copyrightLabel;

        return view('livewire.admin.settings.footer.label');
    }

    public function update()
    {
        $footer = DB::connection('mysql-settings')->table('footers')->limit(1);

        // Validation
        $this->validate([
            'upLabel'        => 'required',
            'widgetLabel1'   => 'required',
            'widgetLabel2'   => 'required',
            'widgetLabel3'   => 'required',
            'rssLabel'       => 'required',
            'socialLabel'    => 'required',
            'supportLabel'   => 'required',
            'phoneLabel'     => 'required',
            'emailLabel'     => 'required',
            'aboutHeadLabel' => 'required',
            'aboutBodyLabel' => 'required',
            'copyrightLabel' => 'required',
        ]);

        // Update
        $footer->update([
            'upLabel'        => $this->upLabel,
            'widgetLabel1'   => $this->widgetLabel1,
            'widgetLabel2'   => $this->widgetLabel2,
            'widgetLabel3'   => $this->widgetLabel3,
            'rssLabel'       => $this->rssLabel,
            'socialLabel'    => $this->socialLabel,
            'supportLabel'   => $this->supportLabel,
            'phoneLabel'     => $this->phoneLabel,
            'emailLabel'     => $this->emailLabel,
            'aboutHeadLabel' => $this->aboutHeadLabel,
            'aboutBodyLabel' => $this->aboutBodyLabel,
            'copyrightLabel' => $this->copyrightLabel,
        ]);

        $this->dispatch('toast', 'success', 'information saved');
    }
}

تابع اپدیت به خوبی کار میکنه فقط مشکل اینه که متد dispatch انگار کلاً کاری انجام نمیده و در نسخه های قبلی لاراول و لایووایر با استفاده از emit فقط 5 دقیقه وقت می گرفت. الان گیج شدم و نمیدونم باید چکار کنم. در اینترنت هم جستجو های زیادی انجام دادم اما فرد دیگه ای مشکل من رو نداره.


ثبت پرسش جدید
حسام موسوی
تخصص : طراح و برنامه نویس
@hesammousavi 7 ماه پیش مطرح شد
0

سلام وقتت بخیر
مستندات ورژن سوم livewire رو بررسی کردی کاملا ؟


کریم کیمیائی فر
تخصص : laravel, php
@KKF 7 ماه پیش مطرح شد
0

سلام وقتتون بخیر.
بله صفحاتی که مربوط به این مبحث هست رو کامل بررسی کردم اما راه حلی برای مشکلم پیدا نکردم.


رایموند
تخصص : مختصص وردپرس - برنامه نویس لار...
@Raymond 7 ماه پیش آپدیت شد
0

درود خوبی...
متد dispatch لایو وایر 3 مشکل داره انگار...
فعلا کد به صورت زیر بنویسید.
کد زیر:
کد فراخوانی toastr:

<script>
        @if(Session::has('message'))
        var type = "{{ Session::get('alert-type','info') }}"
        switch(type){
            case 'info':
                toastr.info(" {{ Session::get('message') }} ");
                break;

            case 'success':
                toastr.success(" {{ Session::get('message') }} ");
                break;

            case 'warning':
                toastr.warning(" {{ Session::get('message') }} ");
                break;

            case 'error':
                toastr.error(" {{ Session::get('message') }} ");
                break;
        }
        @endif
    </script>

کنترلر:

$notification = array(
        'message' => 'دسته جدید با موفقیت ایجاد شد.',
       'alert-type' => 'success'
);

return back()->with($notification);

محمد حسینی
@lu1362 7 ماه پیش آپدیت شد
0

سلام
livewire:load
رو تبدیل کنید به
livewire:init
حل میشه
در این خط باید اعمال بشه

document.addEventListener('livewire:load', () => { // Here

    Livewire.on('toast', (type, message) => {

        Toast.fire({icon: type, title: message})

    })

})

مهدی نظری
تخصص : توسعه دهنده Front End
@mahdi.nazari 7 ماه پیش مطرح شد
0

سلام دوست من 🖐
امیدوارم حالت خوب و عالی باشی✨

مشکلی که شما داری ممکنه به دلیل تفاوت‌های نسخه‌های مختلف لاراول و لایووایر باشع. به عنوان جایگزینی برای متد dispatch میتونی از dispatchBrowserEvent استفاده کنی. در نسخه‌های جدیدتر از لاراول و لایووایر، این متد برای ارسال رویدادهای مرورگر به کد JavaScript کاربر استفاده می‌شه

در متد update خودت، میتونی dispatchBrowserEvent را به این صورت استفاده کنی :

public function update()
{
    $footer = DB::connection('mysql-settings')->table('footers')->limit(1);

    // Validation
    $this->validate([
        'upLabel'        => 'required',
        'widgetLabel1'   => 'required',
        'widgetLabel2'   => 'required',
        'widgetLabel3'   => 'required',
        'rssLabel'       => 'required',
        'socialLabel'    => 'required',
        'supportLabel'   => 'required',
        'phoneLabel'     => 'required',
        'emailLabel'     => 'required',
        'aboutHeadLabel' => 'required',
        'aboutBodyLabel' => 'required',
        'copyrightLabel' => 'required',
    ]);

    // Update
    $footer->update([
        'upLabel'        => $this->upLabel,
        'widgetLabel1'   => $this->widgetLabel1,
        'widgetLabel2'   => $this->widgetLabel2,
        'widgetLabel3'   => $this->widgetLabel3,
        'rssLabel'       => $this->rssLabel,
        'socialLabel'    => $this->socialLabel,
        'supportLabel'   => $this->supportLabel,
        'phoneLabel'     => $this->phoneLabel,
        'emailLabel'     => $this->emailLabel,
        'aboutHeadLabel' => $this->aboutHeadLabel,
        'aboutBodyLabel' => $this->aboutBodyLabel,
        'copyrightLabel' => $this->copyrightLabel,
    ]);

    $this->dispatchBrowserEvent('showToast', ['type' => 'success', 'message' => 'information saved']);
}

بعد در فایل JavaScript خودت، می‌توانید لیسنر رویداد showToast را به صورت زیر تعریف کنی

document.addEventListener('livewire:load', () => {
    Livewire.on('showToast', (type, message) => {
        Toast.fire({icon: type, title: message});
    });
});

این تغییرات باید بهت کمک کنه تا متد dispatch به درستی کار کن و پیامهای خود را با SweetAlert2 نمایش بدی .

امیدوارم پاسخم بهت کمک کرده باشه ❤️
موفق و پیروز باشی 🤘🌹


حسین باقری
تخصص : مهندس الکترونیک
@Hosr 6 ماه پیش مطرح شد
0

ابتدا در فایل composer.json نگاه کنید که نسخه لایوایر شما چیست اگر نسخه روی "livewire/livewire": "^3.0",بود از dispatchاستفاده کنید در غیر اینصورت از متدdispatchEventListenerاستفاده کنید
2-در قسمت پایین صحفه اصلی app.blade.phpدر بخش اسکریپت آخرین نسخه جی کوییری را قرار بدهید اگر از alpineاستفاده می کنید که احتیاجی نیست
3 در قسمت Upgrade Guide در صحفه اصلی مکتوبات لایووالیر درمورد تغییرات کامل dispatchو همجنین روش دوم که با $emitانجام می شود کاملا این مبحث با ذکر مثال گفته شده است


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام