【问题标题】:How can I set named function expressions on my Dropzone options?如何在 Dropzone 选项上设置命名函数表达式?
【发布时间】:2019-05-08 20:28:08
【问题描述】:

我的项目中有多个 Dropzone,它们都非常相似。在其中一个上,我必须创建最小文件宽度,但视频 Dropzone 不适用于文件宽度代码。我最终不得不为每个 Dropzone 创建一个单独的选项调用。这为两个选项调用中的函数创建了许多重复的代码。有没有办法为我的所有选项创建命名函数表达式并使用名称,这样我就没有太多重复的代码?

Dropzone.options[item.substring(1, item.length)] = {
    acceptedFiles: files,
    previewTemplate: document.getElementById('tpl').innerHTML,
    accept: function(file, done) {
        file.acceptDimensions = done;
        file.rejectDimensions = function () { done("Image must be at least 450 pixels wide."); }
    },
    init: function () {
        this.on("thumbnail", function (file) {
            if (file.width !== undefined) {
                if (file.width < 3 * 150) {   // File must be at least 3 blocks wide
                    file.rejectDimensions();
                } else {
                    file.acceptDimensions();
                }
            }
        });
        this.on("success", function (file, response) {
            // code here, including persist variable, which I need to pass in
            generateUIDs(Dropzone.forElement(item).files, persist);
            // more code
        });
    },
    sending: function (file) {
        Dropzone.forElement(item).removeAllFiles();
    },
    removedfile: function (file) {
        // code here, also need persist for this call
        generateUIDs(Dropzone.forElement(item).files, persist);
        //more code
    }
};

理想情况下,我希望accept、send 和removedFile 各为一行以引用命名函数表达式,并传入我的persist 变量。我希望init 事件做同样的事情。

【问题讨论】:

    标签: javascript dropzone.js function-expression


    【解决方案1】:

    我想通了语法:

    Dropzone.options[item.substring(1, item.length)] = {
        acceptedFiles: files,
        maxFilesize: 512,
        previewTemplate: document.getElementById('tpl').innerHTML,
        init: function () {
            this.on("success", function (file, response) { return DropzoneOnSuccess(file, response, item, persist, btn) });
        },
        sending: function (file) {
            Dropzone.forElement(item).removeAllFiles();
        },
        removedfile: function (file) { return DropzoneRemoved(file, item, persist, saver, btn); }
    };
    
    var DropzoneAccept = function (file, done) {
        file.acceptDimensions = done;
        file.rejectDimensions = function () { done("Image must be at least 450 pixels wide."); }
    };
    
    var DropzoneCheckThumbnail = function (file) {
        if (file.width !== undefined) {
            if (file.width < 3 * 150) {   // File must be at least 3 blocks wide
                file.rejectDimensions();
            } else {
                file.acceptDimensions();
            }
        }
    };
    
    var DropzoneOnSuccess = function (file, response, persist) {
        // code
        generateUIDs(Dropzone.forElement(item).files, persist);
        //more code
    };
    
    var DropzoneRemoved = function (file, persist) {
        // code
        generateUIDs(Dropzone.forElement(item).files, persist);
        // more code
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多