问题
I'm having the following code:
<script data-main="scripts/main" src="components/requirejs/require.js"></script>
<script src="scripts/example1.js"></script>
Inside example1
I'm doing a require for some components of which the path is supposed to be set in scripts/main
. However, the path is not correctly set (judging by the console feedback) which leads me to believe that require.js hasn't finished loading files referenced in scripts/main
.
This all is pretty logical (async and all that), but I'd like to know how to run code inside example1.js
after all this is set-up.
Normally, one would simply add the app-init code to scripts/main
probably and be done with it, but I'm building up some examples, which all share the same scripts/main
file (which is pretty big) and I don't want to duplicate that effort.
So how to do this?
回答1:
Ended up doing the following, which isn't ideal (sets a global), but it's ok for my examples.
<!-- sets window.requireconfig with an json object defining paths, etc.-->
<script src="scripts/requireconfig.js"></script>
<!-- in top of example1.js do : require.config(window.requireconfig); -->
<script data-main="scripts/example1" src="components/requirejs/require.js"></script>
来源:https://stackoverflow.com/questions/15386939/how-to-wait-when-data-main-is-loaded-with-require-js-before-executing-script