Javascript assets in a theme component

If you just need Just In Time loading of a javascript asset, like I do here where I require a D3 library, you can use this pattern with Loadscript:

and further up:

Loadscript returns a Promise, so you have to perform a .then on the result. (I’m not certain, but assume the result is implicitly cached, so will run faster second time etc.)

As you can see, you can string them together to load several scripts one after the other.

Note that it’s running inside an Ember Component and is fired off the didInsertElement hook. Learn more about Ember Components here: Templates are HTML - Components - Ember Guides

Note also the script themselves are housed in javacsripts/assets directory.

I based this on @j.jaffeux’s code somewhere used to load in a completely different script (thanks Joffrey! :+1: )

Here’s a Discourse example:

Tip: always dig through the Official and Pavilion TCs and Plugins that are doing something similar to see how they achieve things :).