javascript - Requirejs multiple modules from one file -


i need use 2 modules 1 file.

index.html:

 <!doctype html>     <html>         <head>             <title></title>             <script data-main="app/main" src="app/require.js"></script>         </head>         <body>          </body>     </html> 

main.js:

 require(['modules/message', 'modules/another-module'], function(message, anothermodule) {         alert(anothermodule);     }); 

modules/message.js:

define(function() {     return 'hello there!'; });  define('another-module', function() {     return 'hi there!'; }); 

for reason chrome goes error uncaught error: script error for: modules/another-module

directory structure:

|- appdirectory |-- app |--- modules |---- message.js |--- main.js |--- require.js |-- index.html 

so question is: how can load 2 modules 1 file using 1 require expression? possible?

the issue code when name module define have give full name intend use module. should define('modules/another-module'

but not problem. require this:

require(['modules/message', 'modules/another-module'], 

there multiple ways in can go down there 2 principal ways of interest here:

  1. requirejs loads modules/message before starts trying load modules/another-module. time gets second module, has define it. fine.

  2. requirejs starts loading modules/another-module first. fetch file named modules/another-module.js , won't find it, cause error.

note require call imposes no order on dependencies passed it. requirejs free start loading modules in whatever order wants. can solve second problem using bundles in runtime configuration. instance:

bundles: {     "modules/message": [ "modules/another-module" ] } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -