Projects: Reload-JS (v0.1)

Reload-JS is a javascript live loader whose goal is to be simple and light weight. I created Reload-JS while working on an Infographics Landing Page, and embedding solution, for Newlogic USA. I needed to load and check for any libraries I need (namely jQuery). The loading part has been taken care of in this version, but I am trying to figure out how to include tests for each library, to make sure no override occurs (my jQuery doesn’t break or overwrite the loaded pages jQuery).

More after the jump.

Here is the Reload-JS source code. It is simple and it doesn’t really do anything special. Most of the code has been used previously and is very popular for live loading. It accomplishes its task by using a recursive set of callbacks and has a few ways to call it.

First you can use the following:

  Reload.require("http://example.com/libs/jquery-1.5.3-min.js", 
      function () {
          alert("Weee jQuery is loaded");
  });

Or this

  Reload.requires(["http://example.com/libs/jquery.min.js",  
       "http://www.example.com/underscore.min.js"],  
       function () {
           alert("Weee jQuery and Underscore are loaded");
  });

My personal favorite is this following call:

    Reload.lib_location = "lib";
    Reload.libs = [ 'jquery-1.6.2.min', 'underscore-min',  
                'backbone-min', 'ich-min' ];
    Reload.load( function () {
        $('#jquery_test').html('Testing JQuery Include: Success!');
        var array = [1, 2, 3, 4];
        $("#ul_test").html("<h1>Testing Underscore.js:</h1>");
        _(array).each(function (a) { 
            $('#ul_test').append("<p><b>" + a + ".</b> success!</p>");
        });
    });

You see Reload will automatically append the js if you don’t already have it attached. This is a nice feature to make nice simple calls like above. Reload allows you to set your primary library location and then takes care of it too. This makes loading multiple libraries pretty nice. However, what if you need to use a different host for one of your libraries? No worries! just make sure you use http:// and Reload will detect that it shouldn’t append your default library location to your URL (absolute URL handling).

Reload also has the ability to load from a variable it holds called libs. This can be risky, if multiple people try to use it and is not suggested unless you are sure no one else is using Reload.

Here is the code (and decently commented):

Enjoy!

  1. happyrobotlabs posted this