5 ms·
If I understand this correctly you have to figure out the correct order for the dependencies to be loaded yourself. This does not scale. A better solution for
by tlrobinson 16y ago
If I understand this correctly you have to figure out the correct order for the dependencies to be loaded yourself.
This does not scale. A better solution for larger applications is for individual files to declare their dependencies, then determine the order to load them implicitly.
CommonJS modules are the de-facto standard for doing this on the server, and they're starting to be used in the browser as well.
- Swizec 16y agoThat sounds like a pretty silly drawback. A much cleaner solution would be using window.onload to load all javascript and then only starting execution on document.ready. That way you pretty successfully get around the issue of ordered dependancies because at execution time everything is already loaded. If load.js doesn't do it this way I am profoundly interested as to what the reasoning behind this might be ... perhaps all browser don't differ between window.load and document.ready? (or maybe I'm being stupid in some way or another)
- noibl 16y agodocument.ready fires before window.load
- weixiyen 16y agoWhat you are describing is not lazy-loading. The purpose of lazy loading is to avoid the overhead of having to load all your javascript immediately at the cost of more server requests later on as needed. The benefits of speed gained from lazy-loading can be huge in large applications with little to no browser refresh. The problem with lazy loading javascript in the browser is you have no good way innate to javascript to automatically tell if a script has really been loaded, and the problem you must solve is finding out when you are allowed to execute commands that have dependencies in the lazy loaded scripts.
- Swizec 16y agoThanks for the explanation, was almost certain I was thinking about it wrong :)
- bentlegen 16y agoProbably common knowledge, but RequireJS does this: http://requirejs.org/ http://requirejs.org/
- tlrobinson 16y agoRequireJS uses something similar to CommonJS modules, but they are not CommonJS modules. They use a "wrapped" format that makes it easier to load using script tags in browsers.