onload / onreadystatechange for scripts. Using them, we can uniquely bind some code to the end of loading of a specific module. Things are simple: we need to define this very code in some way. var modules = [
[0, 'item1', function () {
alert ('item1 is loaded');
}],
[1, 'item2', function () {
alert ('item2 is loaded');
}],
[1, 'item3', function () {
alert ('item3 is loaded');
}]
]; 0 if the element is a root and should be loaded immediately), then the file name or its alias. The last is an arbitrary function that can be performed by loading. / * brute force and module loading * /
function load_by_parent (i) {
i = i || 0;
var len = modules.length,
module;
/ * iterate the module tree * /
while (len--) {
module = modules [len];
/ * and load the required elements * /
if (! module [0]) {
loader (len);
}
}
}
/ * declare function loader * /
function loader (i) {
var module = modules [i];
/ * create a new script element * /
var script = document.createElement ('script');
script.type = 'text / javascript';
/ * set the file name * /
script.src = module [1] + '.js';
/ * set the text inside the tag to run on the download * /
script.text = module [2];
/ * remember the current index of the module * /
script.title = i + 1;
/ * set up the download handler for IE * /
script.onreadystatechange = function () {
if (this.readyState === 'loaded') {
/ * iterate through the modules and look for the ones you need to load * /
load_by_parent (this.title);
}
};
/ * set the load handler for the others * /
script.onload = function (e) {
/ * execute the text inside the tag (you need only for Opera) * /
if (/opera/i.test(navigator.userAgent)) {
eval (e.target.innerHTML);
}
/ * iterate through the modules and look for the ones you need to load * /
load_by_parent (this.title);
};
/ * attach the tag to the document * /
document.getElementsByTagName ('head') [0] .appendChild (script);
}
/ * load the root elements * /
load_by_parent (); jsx-component , and then comes the list of components. The library itself bypasses the DOM tree, finds all the modules that need to be loaded, and loads them in sequence. Just great. <div id = "item1" class = "yass-module-utils-base-dom">
<span id = "item2" class = "yass-module-dom" title = "_ ('# item2') [0] .innerHTML = 'component is loading ...';"> </ span>
</ div> yass-module-* class.utils-base-dom and for dom . And in the latter case of loading, in fact, it will not be: the loader waits until the state of the dom component is set to loaded , and only then runs (via eval ) the code recorded in the title this element (in this case, it is a span ).yass.dom.js , yass.base.js and yass.utils.js . By loading all of these modules (because they are called in the dependency chain, in this case the dom depends on the base , which depends on the utils ) the corresponding initialization functions will be called (if defined). Thus, two types of handlers are possible: directly on the component load (to be called for all components in the chain) and after loading the entire specified chain of components (in our case, it is utils-base-dom ).base-callbacks ), which will “freeze” the loading of the base module before receiving callbacks . This can be done (bearing in mind that we expand the dependencies of the base module) as follows: _.load ('callbacks-base'); yass-module-callbacks-base class for an arbitrary element. This will add the desired chain to the dependency tree.dom -> base -> utils -> callbacks
Source: https://habr.com/ru/post/49245/
All Articles