Загрузка скриптов виджета при ajax загрузке

Общие вопросы по использованию фреймворка. Если не знаете как что-то сделать и это про Yii, вам сюда.
Ответить
delgus
Сообщения: 55
Зарегистрирован: 2018.01.29, 14:07
Откуда: Ярославль

Загрузка скриптов виджета при ajax загрузке

Сообщение delgus »

Использую расширение https://www.yiiframework.com/extension/yii-dropzone

Аяксом вызывается экшен контроллера

Код: Выделить всё

 /**
     * Добавить документ
     *
     * @param $id
     * @throws CException
     * @internal param mixed $check
     */
    public function actionAdd($id)
    {
        $id = (int)$id;
        $model = new DocumentsModel;
        $model->statement_id = $id;

      //отключил повторную загрузку скриптов(бубен 1)
     if (Yii::app()->request->isAjaxRequest) {
            Yii::app()->clientScript->scriptMap['jquery.js'] = false;
            Yii::app()->clientScript->scriptMap['jquery-ui.min.js'] = false;
            Yii::app()->clientScript->scriptMap['bootstrap.js'] = false;
        }

        $this->renderPartial('modal_add_document', array(
            'id' => $id,
            'model' => $model
        ), false, true);//выставил proccessOutput true(бубен2)
Код вьюхи modal_add_document

Код: Выделить всё

<form method="post" action="/panels/documents/add/id/<?= $id ?>" enctype="multipart/form-data">
    <div class="modal-header">
        <button style="margin-top:-10px;" type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    </div>
    <div class="modal-body">
        <div class="control-group">
            <label for="" class="control-label">Выберите файл<span class="required">*</span>
            </label>
            <div class="controls">
                <input type="file" name="DocumentsModel[filename]" required id="uploadFile">
            </div>
        </div>
        <?php $this->widget('ext.dropzone.EDropzone', array(
            'name' => 'DocumentsModel[upload]',
            'url' => $this->createUrl('document/add/id/' . $id),
            'onSuccess' => 'alert("iiii")',
            'options' => array(),
        )) ?>
    <div class="modal-footer">
        <button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Отмена</button>
        <button type="submit" class="btn btn-info" ID="submitButton">Отправить</button>
    </div>
</form>
код виджета

Код: Выделить всё

class EDropzone extends CWidget {
    /**
     * @var string The name of the file field
     */
    public $name = false;
    /**
     * @var CModel The model for the file field
     */
    public $model = false;
    /**
     * @var string The attribute of the model
     */
    public $attribute = false;
    /**
     * @var array An array of options that are supported by Dropzone
     */
    public $options = array();
    /**
     * @var string The URL that handles the file upload
     */
    public $url = false;
    /**
     * @var array An array of supported MIME types
     */
    public $mimeTypes = array();
    /**
     * @var string The Javascript to be called in case of a succesful upload
     */
    public $onSuccess = false;

    /**
     * Create a div and the appropriate Javascript to make the div into the file upload area
     */
    public function run() {
        if (!$this->url || $this->url == '')
            $this->url = Yii::app()->createUrl('site/upload');

        echo CHtml::openTag('div', array('class' => 'dropzone', 'id' => 'fileup'));
        echo CHtml::closeTag('div');

        if (!$this->name && ($this->model && $this->attribute) && $this->model instanceof CModel)
            $this->name = CHtml::activeName($this->model, $this->attribute);

        $this->mimeTypes = CJavaScript::encode($this->mimeTypes);

        $options = CMap::mergeArray(array(
                'url' => $this->url,
                'parallelUploads' => 1,
                'paramName' => $this->name,
                'accept' => "js:function(file, done){if(jQuery.inArray(file.type,{$this->mimeTypes})){done('File type not allowed.');}else{done();}}",
                'init' => "js:function(){this.on('success',function(file){{$this->onSuccess}});}"
                ), $this->options);

        $options = CJavaScript::encode($options);

        $script = "Dropzone.options.fileup = {$options}";

        $this->registerAssets();
        Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), $script, CClientScript::POS_END);
    }

    public function registerAssets() {
        $basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
        $baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 1, YII_DEBUG);
        Yii::app()->getClientScript()->registerCoreScript('jquery');
        Yii::app()->getClientScript()->registerScriptFile("{$baseUrl}/js/dropzone.js", CClientScript::POS_END);
        Yii::app()->getClientScript()->registerCssFile("{$baseUrl}/css/dropzone.css");
    }

}
js скрипт подключен(вижу в бразере - что подгружается скрипт с временной точкой /assets/1d0b86fc/js/dropzone.js?_=1543310138915) но почему то не отрабатывает. Какие еще бубны можно попробовать.
Ответить