const EVENT_TEMPLATES_LOADED = "templates:loaded"; const templateNames = [ "class", "home", "species", "about" ]; const partialNames = [ "nav" ]; const loadCount = templateNames.length + partialNames.length; var templates = {}; var contentElement = document.getElementById("content"); var ready = false; function checkLoad(target) { if (Object.keys(templates).length === loadCount) { ready = true; target.dispatchEvent(new Event(EVENT_TEMPLATES_LOADED)); } } templateNames.forEach(function (name) { const path = "templates/" + name + ".hbs"; $.get(path, function (data) { templates[name] = Handlebars.compile(data); checkLoad(contentElement); }, "html"); }); partialNames.forEach(function (name) { const path = "templates/" + name + ".hbs"; $.get(path, function (data) { templates[name] = Handlebars.compile(data); Handlebars.registerPartial(name, templates[name]); checkLoad(contentElement); }, "html"); });