Dropzone JS 等待 ajax 表单提交并更改 options.url

Dropzone JS Wait until ajax form submitted and change options.url

我有 Rails 带有 dropzonejs 插件的应用程序可以上传图像。 我正在上传图片之前创建 Album。 然后我要上传图片到新相册。

Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) {
    # Performing album creation via $.ajax
    # $.ajax return ID of an album, but JS already skipped before I set new URL
    });
  }
};

我如何才能等到请求完成并将返回的 ID 正确设置为 url 选项?

在这种情况下您可以使用 accept -

Dropzone.options.myDropzone = {
  accept: function(file, done)
        {
            file.postData = {};
            $.ajax({
                url: '/album',
                data: {name: file.name, type: file.type},
                type: 'POST',
                success: function(response)
                {
                    file.album_id = response.album_id;
                    done();
                },
                error: function(response)
                {
                    done('error preparing the file');
                }
            });
        },
  init: function() {
    this.on("processing", function(file) {
      # Do other stuffs if needed
    });
  }
};

现在,dropbox 将等待直到 drop() 被调用。