Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Require.js without data-main

Can I use Require.js in development without using the data-main attribute to load in my initial script? ie. <script data-main="scripts/main" src="scripts/require.js"></script> I'm finding it difficult for me to work with this attribute in my development environment.

like image 514
Levi McCallum Avatar asked Oct 12 '12 19:10

Levi McCallum


People also ask

Where do I put RequireJS?

To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.

Should I use RequireJS?

RequireJS is a basic loader, which is used to loads the JavaScript files, it is a framework to manage dependencies between JavaScript files, and in modular programming, all the functionality divides in different modules, so RequireJs is a best tool to assemble different JavaScript files from different modules by which ...

How do you define require in JS?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

What is a RequireJS module?

RequireJS loads each dependency as a script tag, using head. appendChild(). RequireJS waits for all dependencies to load, figures out the right order in which to call the functions that define the modules, then calls the module definition functions once the dependencies for those functions have been called.


1 Answers

Yep, take a look at the documentation: http://requirejs.org/docs/api.html#config

You need to call require.config() and set baseUrl. Based on your example:

<script src="scripts/require.js"></script> <script>     require.config({         baseUrl: "scripts"     });      require( [ /*...*/ ], function(  /*...*/ ) {         /*...*/     }); </script> 
like image 159
Waxen Avatar answered Sep 23 '22 17:09

Waxen