I have a function that looks for the names of the classes to then include the libraries according to the editor to find. It checks the function declaration to know if the library has already been imported.
The code works fine but it repeats.
The question : Is there less simplification of the following code
The problem : I cannot pass variable to the anonymous function of $.getScript
function wfRunEditors(reScann) {
if ($('.wfe-editorjs').length) {
if ('undefined' === typeof initWfEditorJS) {
$.getScript('/src/libs/js/editorjs.full.min.js', function () {
$.getScript('/src/js/editor/wfeditorjs.js', function () {
$.loadCSS('/src/css/editors/editorjs.min.css');
wfCheckerFunction['editorjs'] = true;
initWfeditorjs(reScann);
});
});
} else {
wfCheckerFunction['editorjs'] = true;
initWfeditorjs(reScann);
}
}
if ($('.wfe-trumbowyg').length) {
if ('undefined' === typeof initWfTrumbowyg) {
$.getScript('/src/libs/js/trumbowyg.full.min.js', function () {
$.getScript('/src/js/editor/wftrumbowyg.js', function () {
$.loadCSS('/src/css/editors/trumbowyg.min.css');
wfCheckerFunction['trumbowyg'] = true;
initWftrumbowyg(reScann);
});
});
} else {
wfCheckerFunction['trumbowyg'] = true;
initWftrumbowyg(reScann);
}
}
// add other editors
}
Solution :
- I have minified all the js files
- the init functions and the one that includes the css are called at the end of the minified file
- I globalised the variable reScann
function wfRunEditors() {
let editors = [
'editorjs',
'trumbowyg'
// add other editors
];
for (i = 0; i < editors.length; i++) {
if ($('.wfe-' + editors[i]).length) {
if ('undefined' === typeof window['initWf' + editors[i]]) {
$.getScript('/src/js/editor/' + editors[i] + '.full.min.js');
} else {
wfCheckerFunction[editors[i]] = true;
window['initWf' + editors[i]]();
}
}
}
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment