除非页面刷新,否则Dropzone不会加载

时间:2016-10-07 09:39:23

标签: jquery ruby-on-rails dropzone.js

我在rails应用程序中使用dropzone.js。 dropzone是包含其他输入元素的表单的一部分。在页面加载时,隐藏包含表单的div。点击一个按钮,该div向下滑动。但是,在此阶段Dropzone未初始化。但是,如果我刷新页面并再次单击相同的按钮,它就可以正常工作。

这是我的dropzone函数(在document.ready内)

function dropzoneControl() {
    Dropzone.options.myDropzone = {
        url: '/the_url',
        autoProcessQueue: false,
        uploadMultiple: true,
        parallelUploads: 5,
        maxFiles: 5,
        maxFilesize: 3,
        acceptedFiles: 'image/*',
        addRemoveLinks: true,
        headers: {
            'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        },
        init: function() {
            dzClosure = this;
            document.getElementById("submit-button").addEventListener("click", function(e) {
                e.preventDefault();
                e.stopPropagation();
                dzClosure.processQueue();
            });

            //send all the form data along with the files:
            this.on("sendingmultiple", function(data, xhr, formData) {
                formData.append("name", $("#name").val()); * * other data * *

            });

            //on successful upload
            this.on("successmultiple", function(file, responseText) {
                $('#name').val(""); * * clear the rest of the form * *
            });

            this.on("queuecomplete", function(file) {
                this.removeAllFiles();
            });
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是turbolinks。当我从另一个页面进入页面时,dropzone无法正常工作。但是当我直接刷新它时工作。将data-no-turbolink添加到包含该页面链接的div中,现在效果很好。