How to disable /override dependencies in Yii2

喜夏-厌秋 提交于 2019-12-11 15:30:14

问题


Problem:

The 'kartik-v\yii2-dialog' which is used by 'kartik-v\tree-manager' overrides Sweetalert dialog/message box.

How does one disable the treeview-manager's dependency 'kartik-v\yii2-dialog' in order to use SweetAlerts?

Tried:

'assetManager' => ['bundles' => [ 'kartik\dialog\DialogAsset' => ['js' => [],], ... ,

Sweetalert starts working in grids and confirm events, but then treemanager no longer works (Uncaught ReferenceError: KrajeeDialog is not defined)

In Pictures:

Have:

Want:

Any input would be greatly appreciated.

Update:

Here is the override code, which has worked, but now kartik\yii2-dialog is loaded afterward and overrides this:

yii.confirm = function(message, okCallback, cancelCallback) {
if (message.constructor === Array) {
    swal(
        {
            html: true, // SweetAlert1
            title: message[0],
            text: message[1],
            //html: message[1], // SweetAlert2
            //confirmButtonColor: '#E80000',
            confirmButtonColor: message[3],
            //type: 'warning',
            type: message[2],
            showCancelButton: true,
            cancelButtonText: 'Avbryt',
            closeOnConfirm: true,
            allowOutsideClick: true,
            buttonsStyling: false,
        },
        okCallback
    );
} else {
    swal(
        {
            html: true, // SweetAlert1
            title: message,
            type: 'warning',
            showCancelButton: true,
            cancelButtonText: 'Avbryt',
            closeOnConfirm: true,
            allowOutsideClick: true,
            buttonsStyling: false,
        },
        okCallback
    );
}
};

confirm = function(message, okCallback, cancelCallback) {
    if (message.constructor === Array) {
        swal(
            {
                html: true, // SweetAlert 1
                title: message[0],
                text: message[1],
                //html: message[1], // SweetAlert2
                //confirmButtonColor: '#E80000',
                confirmButtonColor: message[3],
                //type: 'warning',
                type: message[2],
                showCancelButton: true,
                cancelButtonText: 'Avbryt',
                closeOnConfirm: true,
                allowOutsideClick: true,
                buttonsStyling: false,
            },
            okCallback
        );
    } else {
        swal(
            {
                html: true, // SweetAlert 1
                title: message,
                type: 'warning',
                showCancelButton: true,
                cancelButtonText: 'Avbryt',
                closeOnConfirm: true,
                allowOutsideClick: true,
            },
            okCallback
        );
    }
};

yii.alert = function(message, okCallback, cancelCallback) {
    swal(
        {
            title: message,
            type: 'warning',
            showCancelButton: false,
            closeOnConfirm: true,
            allowOutsideClick: false,
            buttonsStyling: false,
        },
        okCallback
    );
};

alert = function(message, okCallback, cancelCallback) {
    swal(
        {
            title: message,
            type: 'warning',
            showCancelButton: false,
            closeOnConfirm: true,
            allowOutsideClick: false,
            buttonsStyling: false,
        },
        okCallback
    );
};

回答1:


Although there is an option provided as krajeeDialogSettings in TreeView which controls the yii2-dialog via using,

'krajeeDialogSettings' => ['overrideYiiConfirm' => true, 'useNative' => true],

According to the docs it should work but for me, it didn't worked, and the yii2-dialog always overrode the sweetalert confirm, I wanted to rule out the prompt or the yii2-dialog from the treeview and for that removing the dependency is not that straight forward because the calls are nested and integrated in the Treeview script.

So, I had to override the krajeeDialog.confirm where i was loading the TreeView widget so that whenever the krajeeDialog.confirm is called my custom confirm dialog would be called.

Just add the below on the top of the view where you are loading the TreeView widget.

<?php 

 $js = <<< JS

 krajeeDialog.confirm = function (message, callback) {
    swal({
        title: message,
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        allowOutsideClick: true
    }, callback);
}
JS;
    $this->registerJs($js, yii\web\view::POS_READY);

Although i didnt like the dual approach but that was the only one that worked for me, maybe someone else could post a better solution.



来源:https://stackoverflow.com/questions/55118032/how-to-disable-override-dependencies-in-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!