我的项目中有多个非常相似的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
}
};
理想情况下,我希望接受,发送和removeFile成为一行,以引用命名的函数表达式,并传入我的persistant变量。我希望init事件做同样的事情。 / p>
答案 0 :(得分:0)
我弄清楚了语法:
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
};