Skip to content

Instantly share code, notes, and snippets.

@VirtuosiMedia
Created November 16, 2013 21:46
Show Gist options
  • Save VirtuosiMedia/7505760 to your computer and use it in GitHub Desktop.
Save VirtuosiMedia/7505760 to your computer and use it in GitHub Desktop.
This is a proof-of-concept for a JavaScript Autoloader. It works, but I haven't yet been able to figure out how to recover from the error without having to re-execute all of the app function. Ideally, it would deal with the error and then try that line again.
<!doctype html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
var app = function(){
console.log('Initialize App');
var test = new Test();
var foo = new Foo();
var bar = new Bar();
};
var autoload = function(app){
var recover = function(error){
var name = error.message.split(' ')[0];
console.log('Loading '+name);
//A file could be synchronously loaded here instead
this[name] = function(){
console.log(name+' has been dynamically created');
};
load(app);
};
var load = function(app){
try {
app();
} catch (error){
if (error.name == "ReferenceError"){
console.log(error.message);
recover(error, app);
}
}
};
load(app);
};
autoload(app);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment