Skip to content

Instantly share code, notes, and snippets.

@gordonmzhu
Last active December 20, 2016 20:46
Show Gist options
  • Save gordonmzhu/c6a85cad30ab2f645502ef1fb471ac49 to your computer and use it in GitHub Desktop.
Save gordonmzhu/c6a85cad30ab2f645502ef1fb471ac49 to your computer and use it in GitHub Desktop.
Simple Library Loader
// (function() {
// var libraryStorage = {};
// function librarySystem(libraryName, callback) {
// if (arguments.length > 1) {
// libraryStorage[libraryName] = callback();
// } else {
// return libraryStorage[libraryName];
// }
// }
// window.librarySystem = librarySystem;
// })();
(function() {
var libraryStorage = {};
function librarySystem(libraryName, dependencies, callback) {
if (arguments.length > 1) {
if (dependencies.length > 0) {
var dependenciesArray = [];
dependencies.forEach(function(dependency) {
dependenciesArray.push(libraryStorage[dependency]);
});
}
libraryStorage[libraryName] = callback.apply(null, dependenciesArray);
} else {
return libraryStorage[libraryName];
}
}
window.librarySystem = librarySystem;
})();
librarySystem('router', [], function() {
return {
description: 'Supposedly, I can handle app routing.',
dependencies: [arguments]
};
});
librarySystem('app', ['router'], function(router) {
return {
description: 'Just a silly app that has a dependency',
dependencies: [arguments]
};
});
console.log(librarySystem('app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment