Javascript Class Loader Equivalent

In Java the class loader dynamically loads Java classes into the JVM. In Javascript , I often have a problem with a call being made with an unloaded dependency, because loading is asynchronous.

I am using Angular JS framework. For example, one page is API dependent, but receives the Cannot read property 'realestate' of undefined error message when called

 gapi.client.realestate.get(propertyId).execute(function(resp) { console.log(resp); }); 

because the API is not loaded. Download using

 loadRealEstateAPI = function() { var ROOT = 'http://localhost:8888/_ah/api'; gapi.client.load('realestate', 'v1', function() { console.log("Real Estate API loaded"); $rootScope.$broadcast("reAPILoaded", true); }, ROOT); } 

I would like to know if there is a JS library that allows an application to start when all dependencies are loaded.

+4
source share
2 answers

RequireJS does just that , letting you define your dependencies and then execute your code only after the dependencies are loaded. If you use something like the Google Maps API that loads in several dependencies of your own, there is even a plugin that will wait for all third third-party dependencies to load.

There is a short entry that shows you how to integrate AngularJS with RequireJS . As an added bonus , the Requirement comes with an optimizer that can be performed as part of the deployment build process to compile all your local files into a single file.

+2
source

I wrote a tool that does just that. It detects and manages your parsing dependencies, as well as the usual runtime dependencies.

here: http://damonsmith.imtqy.com/js-class-loader/

it’s a javascript javascript bundle and a dependency detector, not a dependency, like in β€œI want to add jquery to my site,” but a dependency, like β€œI want to manage a very large OO-style JavaScript code base at runtime and parsing -dependencies written by the development team "It is designed to be installed and configured very simply and quickly in Java applications and IDEs, and it works for applications that do not support Java. After it is installed in your assembly or on your server, you do not need to worry about it.

0
source

Source: https://habr.com/ru/post/1494831/


All Articles