I have about half a dozen JS class files right now. Instead of putting in a bunch of tags, I used jQuery to load them dynamically--in order--like so:
var classes = ["A","B","C"...];
blah.load = function(callback) {
for (var i in classes) {
console.log("Loading "+blah.RootURL+"/"+classes[i]+".js");
$.ajax({
async: true,
url: blah.RootURL+"/"+classes[i]+".js",
dataType: "script",
error: function(jqxhr, status, error) {
console.log("Unable to load "+classes[i]+": "+status+", "+error);
},
success: function(data, status, xhr) {
if (window.tmpLoadCount) {
window.tmpLoadCount++;
} else {
window.tmpLoadCount = 1;
}
if (window.tmpLoadCount == classes.length) {
console.log("Initializing...");
callback();
}
}
});
}
};
Currently I'm using multiple script tags so I can keep working. The problem I had with the jQuery approach was that sometimes, when [re]loading, previous scripts didn't seem to execute (or finish executing) before the next script was loaded, even with async: true. My classes are pretty short and simple. I would see the script load via the "Loading" log, but then later see an error like "Unable to load B: A is not a constructor" (if B inherits from A), which suggests to me that A wasn't fully loaded when the B.prototype=new A(); line was executed.
I found one reference to this online, and that suggested adding a setTimeout() call to give the engine some time, and suggested that this only happened when processing stuff in jQuery's "success" callback...unfortunately I'm not sure how to ensure that everything gets loaded in order in a dynamic way without using the success callback.
Aside from many <script/> tags or minifying (which makes debugging harder) or requiring class changes, does anyone have suggestions on how to cleanly avoid this "race condition"? My current thought, which I don't really like, is to have the final line in each class file add it's class name to a global static property, and have the ajax call use an interval to check that property to determine if it should load the next file...bleh.