Note See also GitHub repo for source code.
To simplify loading of CSS and JS libraries. The API Loader automatically works out the order the libraries should be loaded based on dependencies between them. It also skips libraries that already exist on a page or that pass a specific check.
Note If you are passing a string or an array as the first parameter into the library, these are @deprecated but still working for backwards compatibility.
First you require the API Loader. You can, for example, use the following shorthand notation that always points to the latest version.
Now, you can use the loader by passing in an object that looks for example like so:
The object works like so:
- You pass in either a
- The key inside the object, like
jQuerythen refers to your library. If this key is on a
windowobject (as is the case with jQuery library), we won't load the library since it already exists.
- If you do not like the previous check and want something more robust, pass a sync function under the
trueif a library should NOT be loaded.
pathrepresents the URL pointing to the library.
dependskey, passing an array if a library depends on other libraries in your list. In the example, you can see that
_(underscore.js). The appropriate loading order will be worked out from this.
- Check the
errvariable passed in the callback function (second parameter).