All notes
Requirejs

Intro

requirejs.org.

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node.

It implements the Asynchronous Module API.



<!-- ---------- main.html -->

<!-- data-main attribute tells require.js to load
     scripts/main.js after require.js loads. -->
<script data-main="scripts/main" src="scripts/require.js"></script>

<!-- ---------- main.js -->

<!-- Inside of main.js, you can use requirejs() to load any other scripts you need to run. This ensures a single entry point, since the data-main script you specify is loaded asynchronously. -->

requirejs(["helper/util"], function(util) {
    //This function is called when scripts/helper/util.js is loaded.
    //If util.js calls define(), then this function is not fired until util's dependencies have loaded, and the util argument will hold the module value for "helper/util".
});

API

define



define(
module_id /*optional*/, 
[dependencies] /*optional*/, 
definition function /*function for instantiating the module or object*/
);

// Example

define('myModule', 
  ['foo', 'bar'], 
  // module definition function
  // dependencies (foo and bar) are mapped to function parameters
  function ( foo, bar ) {
    // return a value that defines the module export
    // (i.e the functionality we want to expose for consumption)
  
    // create your module here
    var myModule = {
      doStuff:function(){
        console.log('Yay! Stuff');
      }
    }

    return myModule;
});