karnbrockgmbh / yii2-modal-ajax

MIT License
13 stars 8 forks source link

JS scripts should be loaded in order #8

Closed stupidusername closed 5 years ago

stupidusername commented 7 years ago

I don't really know for sure if this is a real issue or not but I've got a problem due to JS scripts being executed as soon as they are downloaded. I think JS scrips should be executed in the exact order as they appear on script tags. I changed injectHtml method to something like this on its last lines and it looks like the problem is gone.

var loadNextScript = function() {
    jQuery.getScript(newScripts[loadedScriptsCount] + (new Date().getTime()), function () {
        loadedScriptsCount++;
        if (loadedScriptsCount === newScripts.length) {
            scriptsLoaded();
        } else {
            loadNextScript();
        }
    });
}

/**
 * Scripts loaded callback
 */
var scriptsLoaded = function () {
    // Execute inline scripts
    for (var i = 0; i < inlineInjections.length; i += 1) {
        window.eval(inlineInjections[i]);
    }
};

// Load scripts
if (newScripts.length) {
    loadNextScript();
} else {
    scriptsLoaded();
}
stupidusername commented 5 years ago

Closed due to inactivity.