I need to include a third-party js library in my website and a working example in plain HTML file is something like
<div id="app"></div>
<script src="app.js"></script>
<script>App.initialize();</script>
Basically the initialize() function will first fill the #app with a loading animation then fetch data from external API and finally load the #app with html code it generated.
Everything works fine until I rewrite it in mithril.js, something like
const page = {
view: () => [
m('script', {src: 'app.js'}),
m(app)
]
}
const app = {
oncreate: () => { App.initialize();},
view: () => m('#app')
}
When I render the page, I can still see the loading animation but after it is gone, nothing shows up in #app. The expected code is not loaded in #app as in plain HTML file, and there is no error message in the console.
Did I miss anything or do anything wrong in mithril.js?
I think there might be some sort of timing issue with loading the script that way. Also if it puts things into the body element then mithril might re-render over it. Maybe try something like this:
More information here, the scripts injected are loaded async by default. You can test this in browser with
document.createElement('script').async.https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement#dynamically_importing_scripts