Lazy Loading Modules
Finally, we will get an introduction to the advanced technique of Lazy Loading Modules. The key concept of this practice is that, during the page load, the browser will only download and execute those modules that are required for the initial rendering of the page while the rest of the application modules are requested after the page is fully loaded and is required to respond to a user action. RequireJS
(http://requirejs.org/) is a popular JavaScript library that is used as a module loader but, for simple cases, we can achieve the same result with jQuery.
As an example of this, we will use it to lazy load the informationBox
module of the Dashboard example that we saw in previous chapters, after the first click of the user on the Dashboard's <button>
. We will abstract the implementation that is responsible for downloading and executing JavaScript files into a generic and reusable module named moduleUtils
:
(function() { 'use strict'; dashboard...