23

I am trying to learn how to use require.js. So I made an HTML page with the following tags in the body.

<script type="text/javascript" data-main="../js/shirt" src="../js/require.js"></script>
<script type="text/javascript">
    alert("Shirt color is " + shirt.color);
</script>

The ../js/shirt.js has the following code

define({
    color: "black",
    size : "large"
});

How can I use this simple value pairs in my html?

1
  • why are you explicitly including require.js like <script type="text/javascript" data-main="../js/shirt" src="../js/require.js"></script> rather than using browserify? Commented Jun 28, 2017 at 8:58

3 Answers 3

34

In addition to Domenic's answer maybe you prefer this way of using the define function without using require functions inside the modules.

// shirt.js
define({
    color: "black",
    size : "large"
});

// logger.js
define(["shirt"], function (shirt) {
    return {
        logTheShirt: function () {
            console.log("color: " + shirt.color + ", size: " + shirt.size);
        }
    };
});

// main.js
define(["shirt", "logger"],function (shirt, logger) {    
    alert("Shirt color is: " + shirt.color);
    logger.logTheShirt();
});

I prefer this way, but it's only a matter of taste I guess. (I'm assuming that all the scripts are on the same folder.)

Sign up to request clarification or add additional context in comments.

2 Comments

Why does main.js also have "define" code? Isn't this code meant for creating modules?
@Kokodoko: main.js is a module itself, and it depends on the other two modules, it doesn't creates them. Modules are "created" or defined using the define function.
19

In addition to Joseph's answer, you can also write other modules that depend on shirt (which is where the real power of RequireJS comes in).

// shirt.js
define({
    color: "black",
    size : "large"
});

// logger.js
define(function (require) {
    var shirt = require("./shirt");

    return {
        logTheShirt: function () {
            console.log("color: " + shirt.color + ", size: " + shirt.size);
        }
    };
});

// main.js
define(function (require) {
    var shirt = require("./shirt");
    var logger = require("./logger");

    alert("Shirt color is: " + shirt.color);
    logger.logTheShirt();
});

Then your HTML can just be

<script data-main="../js/main" src="../js/require.js"></script>

1 Comment

Why does main.js also have "define" code? Isn't this code meant for creating modules?
16

the contents of the main file should be a require call. for example, you have a values.js module containing:

define({
    color: "black",
    size : "large"
});

in your main file (shirt.js), load the values.js as a dependency (assuming they are in the same directory):

require(['values'],function(values){
    //values.color
    //values.size
});

3 Comments

I changed 'alert("Shirt color is " + shirt.color);' line to 'require(["shirt"],function(shirt){alert("Shirt color is " + shirt.color);});', and I got what I was looking for. Thank your for your example.
One suggestion. The file names in your answer do not correspond to the file names in the question. Better to use the file names in the question..or use altogether different ones.
But what if you need those values later? Do you have to load them again with a new require call?

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.