简体   繁体   中英

How to wait when data-main is loaded with Require.JS before executing script?

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM