如何在我的 Dropzone 选项上设置命名函数表达式?
How can I set named function expressions on my Dropzone options?
我的项目中有多个非常相似的 Dropzones。其中之一,我必须创建一个最小文件宽度,但视频 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、sending 和 removedFile 分别在一行中引用命名函数表达式,并传入我的持久变量。我希望 init 事件做同样的事情.
我弄明白了语法:
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
};
我的项目中有多个非常相似的 Dropzones。其中之一,我必须创建一个最小文件宽度,但视频 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、sending 和 removedFile 分别在一行中引用命名函数表达式,并传入我的持久变量。我希望 init 事件做同样的事情.
我弄明白了语法:
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
};