Created
July 20, 2024 16:28
-
-
Save vordan/6e23a007c570f3f25cb0b4a448528c63 to your computer and use it in GitHub Desktop.
Asynchronous function to load a module-specific configuration script, execute it, and retrieve the configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Asynchronous function to load a module-specific configuration script, execute it, and retrieve the configuration. | |
* | |
* Requirements: | |
* - URL parameter named 'module' which specifies the module name or pass the module name as an argument. | |
* - The module name in the URL or the argument can contain dashes which will be replaced with underscores to form the variable name. | |
* - Each module-specific script should define an asynchronous configuration variable using the naming convention: | |
* 'module_name_tabulator_edit_config'. | |
* - The scripts should be located at: 'session_data.app_http_root + local/tables/tabulator-edit/{module}.tabulator.js'. | |
* | |
* Expected Outcome: | |
* - The configuration variable (e.g., 'personalno_dosie_osnovni_podatoci_tabulator_edit_config') is loaded and assigned to | |
* 'pub.tabulator_config' for further use. | |
* | |
* Usage: | |
* - Call this function within an asynchronous context to ensure the configuration is loaded before use. | |
* | |
* Example URL: | |
* - https://example.com/page?module=personalno-dosie-osnovni-podatoci | |
*/ | |
async function loadConfigurationAndData(moduleName = null) { | |
// Get the module name from the URL or from the argument | |
let module = new URLSearchParams(window.location.search).get('module') || moduleName; | |
// Fail if both URL parameter and argument are not provided | |
if (!module) { | |
console.error('Module name not provided in URL or argument.'); | |
return; | |
} | |
// Replace dashes with underscores | |
const moduleNameWithUnderscores = module.replace(/-/g, '_'); | |
const scriptUrl = `${session_data.app_http_root}local/tables/tabulator-edit/${module}.tabulator.js`; | |
try { | |
// Fetch the module-specific script content | |
const response = await fetch(scriptUrl); | |
if (!response.ok) { | |
throw new Error(`Failed to fetch script: ${response.statusText}`); | |
} | |
const scriptContent = await response.text(); | |
// Safely evaluate the script content as an async function | |
const asyncScriptWrapper = `(async () => { ${scriptContent} })()`; | |
await eval(asyncScriptWrapper); | |
// Ensure the configuration variable is defined | |
const configVariableName = `${moduleNameWithUnderscores}_tabulator_edit_config`; | |
if (typeof window[configVariableName] === 'undefined') { | |
console.error(`Configuration for module "${module}" not found.`); | |
return; | |
} | |
// Access the configuration variable | |
pub.tabulator_config = await window[configVariableName]; | |
} catch (error) { | |
console.error('Error loading configuration and data:', error); | |
} | |
} | |
// Usage example | |
(async () => { | |
try { | |
await loadConfigurationAndData(); // Use URL parameter | |
// or | |
// await loadConfigurationAndData('personalno-dosie-osnovni-podatoci'); // Use argument | |
console.log('Configuration loaded successfully:', pub.tabulator_config); | |
} catch (error) { | |
console.error('Error in usage example:', error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment