simple example for using require.js

javascript, requirejs

Solution

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
});

Problem

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?

Original source