Yii2 Dropzone 已经附加 &未提供 URL 错误 [英] Yii2 Dropzone already attached & No URL Provided error

查看:32
本文介绍了Yii2 Dropzone 已经附加 &未提供 URL 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 yii2 项目使用 dropzone 扩展.

我使用了 https://github.com/perminder-klair/yii2-dropzone

我的控制器:

<?php echo \kato\DropZone::widget(['选项' =>['网址' =>'/dobuyme/frontend/web/index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat','maxFilesize' =>'2',],'clientEvents' =>['完成' =>功能(文件){console.log(文件)}",'removedfile' =>"function(file){alert(file.name + '被删除')}",],]);

我收到错误:

<块引用>

dropzone.min.js:1 未捕获的错误:未提供 URL.在新的 c (dropzone.min.js:1)在 Function.a.discover (dropzone.min.js:1)在 a._autoDiscoverFunction (dropzone.min.js:2)在 HTMLDocument.f (dropzone.min.js:2)未捕获的错误:Dropzone 已附加.在新的 c (dropzone.min.js:1)在 HTMLDocument.<匿名>(index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat:962)在 mayThrow (jquery.js:3557)在进程中 (jquery.js:3625)

解决方案

故障

您使用的扩展存在实现错误,当您在没有 jquery 的情况下初始化放置区时,放置区初始化不应在 jquery 函数或 onready 内, 表示如果您使用

new Dropzone();

那么你最好将脚本添加到页面末尾的 </body> 标签之前,不要将其包裹在任何这些标签中

  • $(document).ready(function(){})

  • jQuery(function ($) {});

如果您查看正在使用 dropzone 的页面的视图源,您正在使用的扩展程序不是这种情况,它会向您显示类似这样的内容

jQuery(function ($) {Dropzone.autoDiscover = false;var myDropzone = new Dropzone("div#myDropzone", {"maxFilesize": "2","url": '/dobuyme/frontend/web/index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat',"previewsContainer": "#previews",可点击":真实,标题":{"X-CSRF-Token": "mNq_nlbRIYljqGTXp-bB3nzJelGfikZUzhpLwUapdxbSjMntFZxi5yDgEJjet4WSP60YCc_Gfh6nfAGrdsAbLg=="},参数":{"_csrf": "mNq_nlbRIYljqGTXp-bB3nzJelGfikZUzhpLwUapdxbSjMntFZxi5yDgEJjet4WSP60YCc_Gfh6nfAGrdsAbLg=="}});myDropzone.on('完成',函数(文件){控制台日志(文件)});myDropzone.on('removedfile', function (file) {警报(文件名 + '被删除')});});

和我上面提到的有同样的问题.

错误来源

源码类下面一行需要更正DropZone.php

$view->registerJs($js);

默认情况下,Yii2 将脚本包装在函数中,除非您在 registerJs() 函数的第二个参数中指定,在上述情况下应该是 \yii\web\View::POS_END,构成一行.

$view->registerJs($js, $view::POS_END);

解决方案

您目前可以做的是扩展类并覆盖 registerAssets() 函数,并更改该行,以便您要做的是.

  1. common/components文件夹内创建一个DropZone.php文件,如果没有名为/components的文件夹在项目根目录的 common/ 文件夹中创建一个,并在文件中添加以下代码.

    命名空间 common\components;使用 yii\helpers\Json;使用 kato\assets\DropZoneAsset;使用 kato\DropZone 作为 KatoDropZone;类 DropZone 扩展了 KatoDropZone{公共函数 registerAssets(){$view = $this->getView();$autoDiscover = $this->autoDiscover;$id = $this->id;$dropzoneContainer = $this->dropzoneContainer;$options = Json::encode($this->options);$js = <<<JSDropzone.autoDiscover = $autoDiscover;var $id= new Dropzone("div#$dropzoneContainer",$options);JS;if (!empty($this->clientEvents)) {foreach ($this->clientEvents as $event => $handler) {$js .= "$this->id.on('$event', $handler);";}}$view->registerJs($js, $view::POS_END);DropZoneAsset::register($view);}}

  2. 现在在视图中使用您的自定义类并将名称空间从 \kato\DropZone::widget() 更改为 \common\Dropzone::widget()代码> 就是这样.它应该工作顺利,您的视图代码应该如下所示.

    echo \common\DropZone::widget(['选项' =>['网址' =>'/dobuyme/frontend/web/index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat','maxFilesize' =>'2',],'clientEvents' =>['完成' =>功能(文件){console.log(文件)}",'removedfile' =>"function(file){alert(file.name + '被删除')}",],]);

I'm trying to use dropzone extension for my yii2 project.

I used https://github.com/perminder-klair/yii2-dropzone

My controller :

<?php echo \kato\DropZone::widget([
    'options' => [
        'url' => '/dobuyme/frontend/web/index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat',
        'maxFilesize' => '2',

    ],
    'clientEvents' => [
        'complete' => "function(file){console.log(file)}",
        'removedfile' => "function(file){alert(file.name + ' is removed')}",
    ],
]);

I'm getting error :

dropzone.min.js:1 Uncaught Error: No URL provided.
    at new c (dropzone.min.js:1)
    at Function.a.discover (dropzone.min.js:1)
    at a._autoDiscoverFunction (dropzone.min.js:2)
    at HTMLDocument.f (dropzone.min.js:2)


Uncaught Error: Dropzone already attached.
    at new c (dropzone.min.js:1)
    at HTMLDocument.<anonymous> (index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat:962)
    at mightThrow (jquery.js:3557)
    at process (jquery.js:3625)

解决方案

Fault

There is an implementation fault in the extension you are using, the dropzone initialization should not be inside the jquery function or onready when you are initializing the drop zone without jquery, means if you are using

new Dropzone();

Then you should better add the script in the end of the page before </body> tag without wrapping it inside any of these

  • $(document).ready(function(){})

or

  • jQuery(function ($) {});

Which isnt the case with the extension you are using if you look into the view source of the page where you are using dropzone it will show you something like this

jQuery(function ($) {
    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone("div#myDropzone", {
        "maxFilesize": "2",
        "url": '/dobuyme/frontend/web/index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat',
        "previewsContainer": "#previews",
        "clickable": true,
        "headers": {
            "X-CSRF-Token": "mNq_nlbRIYljqGTXp-bB3nzJelGfikZUzhpLwUapdxbSjMntFZxi5yDgEJjet4WSP60YCc_Gfh6nfAGrdsAbLg=="
        },
        "params": {
            "_csrf": "mNq_nlbRIYljqGTXp-bB3nzJelGfikZUzhpLwUapdxbSjMntFZxi5yDgEJjet4WSP60YCc_Gfh6nfAGrdsAbLg=="
        }
    });
    myDropzone.on('complete', function (file) {
        console.log(file)
    });
    myDropzone.on('removedfile', function (file) {
        alert(file.name + ' is removed')
    });
});

and has the same problem as i mentioned above.

Source of error

The correction is needed in the following line in the source class DropZone.php

$view->registerJs($js);

where Yii2 wrap your the script in the function by default unless you specify in the second parameter of the registerJs() function, which in the above case should have been \yii\web\View::POS_END, which make the line.

$view->registerJs($js, $view::POS_END);

Solution

What you can do currently to make it work is to extend the class and override the registerAssets() function, and change the line so what you will do is.

  1. Create a file DropZone.php inside the common/components folder, if there isnt any folder with the name /components inside the common/ folder on the root of your project then create one, and add the following code inside the file.

    namespace common\components;
    
    use yii\helpers\Json;
    use kato\assets\DropZoneAsset;
    use kato\DropZone as KatoDropZone;
    
    class DropZone extends KatoDropZone
    {
        public function registerAssets()
        {
            $view = $this->getView();
            $autoDiscover = $this->autoDiscover;
            $id = $this->id;
            $dropzoneContainer = $this->dropzoneContainer;
            $options = Json::encode($this->options);
    
            $js = <<<JS
            Dropzone.autoDiscover = $autoDiscover;
            var $id= new Dropzone("div#$dropzoneContainer",$options);
    JS;
    
            if (!empty($this->clientEvents)) {
                foreach ($this->clientEvents as $event => $handler) {
                    $js .= "$this->id.on('$event', $handler);";
                }
            }
    
            $view->registerJs($js, $view::POS_END);
            DropZoneAsset::register($view);
        }
    }
    

  2. Now use your custom class in the view and change the namespace from \kato\DropZone::widget() to \common\Dropzone::widget() and thats it. it should work smooth, your view code should look like below.

    echo \common\DropZone::widget([
        'options' => [
            'url' => '/dobuyme/frontend/web/index.php?r=boats/create&city_id=2&cat=rowpaddleboats&subcat=paddleboat',
            'maxFilesize' => '2',
    
        ],
        'clientEvents' => [
            'complete' => "function(file){console.log(file)}",
            'removedfile' => "function(file){alert(file.name + ' is removed')}",
        ],
    ]);
    

这篇关于Yii2 Dropzone 已经附加 &amp;未提供 URL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆