1

In the app working with, I don't have a way of specyfing attributes on the tag for requirejs. I can specify the script tag in the form of

<script type="text/javascript" src="scripts/require.js"></script>

I can append another tag before or after this one and include code to bootstrap my app (main module). I am using jQuery 1.7.1 and would like to load it as dependency in other modules. I also want to do that without the requirejs-jquery bundle, so jQuery seperate of requirejs. I am looking for the right way to do that. I am fairly new to requirejs so an explanation of how the modules are loaded and a code sample would be great. Thanks!

2
  • have you read the jquery tutorial on require.js's documentation? It's very helpful - requirejs.org/docs/jquery.html Commented Feb 17, 2012 at 16:00
  • I have. The examples there do use the data-main attribute and that is where I am getting a little lost. I am hoping that someone has figured out THE right way to do this 'alternative configuration' and is willing to share here. Commented Feb 17, 2012 at 16:15

1 Answer 1

3

include another javascript file after requirejs.

<script type="text/javascript" src="scripts/require.js"></script>
<script type="text/javascript" src="scripts/main.js"></script>

In this file you have to configure requirejs so it finds jquery.

main.js

require.config({
    paths: {
        "jquery": "js/jquery-1.7.1.min"//edit this path so it suits your needs.
  } 
});

From now on you'll be able to load jquery into your Modules by

require(['jquery'], function ($) {
    $('#foo').show();
});
Sign up to request clarification or add additional context in comments.

5 Comments

Could you explain where that last require belongs? Is that in main.js or another module? If latter, how do you tell it to load that in main.js?
After requirejs is loaded, you can use require() wherever you want to. This can be the main.js or any other js you load.
Thanks! I ended up renaming main.js to requirejs-config.js with require(["main"]) call at the end. That way requirejs-config.js configures the paths and tells requirejs which file to load first. The main file then has no bootstrapping/configuration logic.
This does open up the possibility of errors - main.js depends on require.js and occasionally you may find that main.js is loaded before require.js which results in a define is not defined error.
Loading scripts is synchronous. If you don't add a async attribute to the script element loading requirejs, it will be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.