R.js

ultralight solution to dependencies management, freely inspired by RequireJs and Angular dependency injection mechanism.


Hello World.



    //defines HelloWorld; its dependencies are: "Hello" and "World"; gets resolved when both "Hello" and "World" becomes defined.
    R("HelloWorld", ["Hello", "World"], function (Hello, World) {
      return Hello + " " + World;
    });

    //defines Hello
    R("Hello", [], function () {
      return "Hello";
    });

    //defines World
    R("World", [], function () {
      return "World";
    });

    

Asynchronous definition.



    //defines "SomeModule", its dependency is "LateModule"; gets resolved when "LateModule" becomes defined
    R("SomeModule", ["LateModule"], function (LateModule) {
      //this function is called when the "LateModule" becomes available
      //for example, this may happen when loading dynamically JavaScript with an AJAX call
      console.log("LateModule was resolved!");
      return "";
    });

    window.setTimeout(function () {
      //defines "LateModule" after 5 seconds
      R("LateModule", [], function () {  
        return "foo";
      });
    }, 5e3);

    

Just functions.



    +function () {
      var k;
      
      //"Qualcosa" doesn't return anything, but depends upon "Something", therefore its function is called after "Something" function.
      R("Qualcosa", ["Something"], function () {
        //gets fired after "Something" function
        console.log("@", k);
      });

      //defines "Something": its function does not return anything; but interacts with external variable "k"
      R("Something", [], function () {
        k = 30;
      });
    }();

    

Keys obfuscation with robscure.

If you desire, you can obfuscate the keys defined using R.js, to make your code even smaller and more secret.

Follow the instructions described here robscure.



      //in your GruntFile, define a configuration element for robscure, defining the areas files (it is recommended to use minified files)

      grunt.initConfig({
        robscure: {
          website: {
            // these are all the built or minified files of all areas: the task will look for each
            // for each file, a new file will be generated, with obfuscated module names
            areas: [
              "../scripts/main.min.js",
              "../scripts/area-one.min.js",
              "../scripts/area-two.min.js",
              "../scripts/area-three.min.js"
            ]
          }
        }
      });

      //************************************************************************************************
      //after obfuscation of keys, the keys get converted from:
      R("model",["extend","events"],function(a,b){var c=function(a,b){b&&_.extend...

      //to... (something like)
      R("♠",["♫","♦"],function(a,b){var c=function(a,b){b&&_.extend...