EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
3.99k stars 1.01k forks source link

Allows ToggleSwitch to dispatch a subscribable event #6266

Open Geolim4 opened 4 weeks ago

Geolim4 commented 4 weeks ago

Short description of what this feature will allow to do:

In assets/js/field-boolean.js it would be great if the Promise could dispatch a "subscribable" event:

Replace

        .then(() => { /* do nothing else when the toggle request is successful */ })
        .catch(() => this.#disableField());

by

        .then((responseText) => {
            document.dispatchEvent(new CustomEvent('ea.form.ajax.switch', {
                cancelable: true,
                detail: { response: responseText, field: this.field, error: null}
            }));
        })
        .catch((reason) => {
            this.#disableField();
            document.dispatchEvent(new CustomEvent('ea.form.ajax.switch', {
                cancelable: true,
                detail: { response: null, field: this.field, error: reason}
            }));
        });

What do you thing @javiereguiluz ?

So we can customize the behavior (e.g: with a toaster) and subscribe to the event as we would like to