I have a very simple html page where I put this script at the end:
<?php echo $this->Html->script(['studiomain.js']); ?>
</html>
The script contains an IIF in JS:
window.studiomain = window.studiomain || (function ($) {
let _dataTable = '';
let _modalTemplates = {};
let _webroot = 'studioasq';
function setDataTable (t, options={}) {
_dataTable = $(t);
if (typeof $(t).DataTable == 'function') {
options.language = {
"url": "/" + _webroot + "/js/datatable/i18n/Italian.json"
}
$(t).DataTable(options);
}
}
function setModal(key='',template='') {
_modalTemplates[key] = template;
}
function renderModal(key,data={}) {
if (_modalTemplates[key] !== undefined) {
let copy = _modalTemplates[key];
Object.keys(data).forEach((key) => {
copy.replace(new RegExp("{{" + value + "}}","g"),data[key]);
})
}
return $('#'+key);
}
return {
setDataTable,
setModal,
renderModal
}
})($);
But when the page finishes loading, I have no studiomain in window:
window.studiomain => undefined.
I think the problem is the renderModal function: If I delete it all is fine. What am I missing?
**** UPDATE ****
Following suggestions, I think the problem is in the order of loading scripts and passing the reference to JQuery.
I discovered also that passing (jQuery) and NOT ($) to the IIF works.