Commits

Scott Koon committed 4c86077 Merge

merged from future branch

  • Participants
  • Parent commits 2c32b97, a9cd254

Comments (0)

Files changed (6)

 
 $b("script file name.js","id you want the script element to have");
 
+You can also not pass in an id and Bootstrap will assign a random one to your script tag
+
 alternatively, you can pass in a call back function.
 $b("script file name.js", "id of your element", callBackFunction);
 
 
 var bootstrap = (function() {
     var itemsLoaded = []; //maintain a list of items we have already loaded to prevent duplicates
-    var bootstrap = function(scriptSrc, id, callback) {
+    var bootstrap = function() {
+        var args = arguments;
+        if(args.length == 0) return;
+
+        var scriptSrc = "";
+        //figure out if we were passed a script URL, a config object, or an array of script URLs
+        var src = args[0];
+        console.log(typeof src);
+
+        if(typeof src === "string") {
+            scriptSrc =src;
+        } else if(typeof src == "object" || typeof src == "array") {
+            processConfigObject(src);
+        }
+
         var head = document.head || document.getElementsByTagName("head")[0];
-        var scriptId = id || "script" + Math.floor(Math.random() * 1234);
+        var scriptId = (function() { if(args.length==2) { return args[1];} else {return false;}})() || "script" + Math.floor(Math.random() * 1234);
         var loaded = false;
         var scriptTag = null;
-
+        var callback = null;
+        if(args.length == 3) callback = args[2];
 
         function notLoaded(src, type) {
             var nodeList;
 
         function makeCallback() {
             window.clearInterval(id);
-            if (callback != undefined) {
+            if (callback != null) {
                 callback();
             }
         }
         return link;
     }
 
+    function processConfigObject(config) {
+        for(var script in config) {
+            bootstrap(config[script], script);
+        };
+    }
     return (window.bootstrap = window.$b = bootstrap);
 }())

bootstrap_test.html

             }}, 10) 
     });
 
-    bootstrap("test.js","test2");
+bootstrap("test.js");
+bootstrap({ "test3":"test3.js"});
+bootstrap(["test4.js"]);
 
 </script>
 </body>
 
 testSuite.add(function() {
     var scripts = document.getElementsByTagName("script");
-    return (scripts.length==4);
+    return (scripts.length==3);
 }
         , "Are all 4 scripts loaded?"
         , "All 4 scripts are not loaded.")
         , "Is test.js loaded with an id of 'test'?"
         , "bootstrap did not load test.js properly");
 
+testSuite.add(function() {
+        return (document.getElementById("0") != null);
+        }
+        , "Can add a script by passing in an array"
+        , "Could not add a script by passing in an array");
+
+testSuite.add(function() {
+        return (document.getElementById("test3") != null);
+        }
+        , "Can add a script by passing in a config object"
+        , "Could not add a script by passing in a config object");
+
 //At some point, I'll put this test back in to see how bootstrap should handle a missing JS file
 /*testSuite.add(function() {
     var scripts = document.getElementsByTagName("script");
         , "Was the callback called?"
         , "The callback was not called.");
 
+testSuite.add(function () {
+        var scripts = document.getElementsByTagName("script");
+        for(var i = 0;i<scripts.length;i++) {
+            if(scripts[i].src.indexOf("test.js") > -1 && scripts[i].id == "") return false;
+        }
+        return true;
+        }
+        ,"An id was added to all of the scripts"
+        ,"An id was not added to all of the scripts");
+
 testSuite.add(function() {
     var nl = document.getElementsByTagName("script");
     var count = 0;
     return (count==1);
 }
         , "Was test.js loaded twice?"
-        , "test.js was loaded more than once");
+        , "test.js was loaded more than once");

test3.js

Empty file added.

test4.js

Empty file added.