assignment.js 971 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const EVENT_TEMPLATES_LOADED = "templates:loaded";
  2. const templateNames = [
  3. "class",
  4. "home",
  5. "species",
  6. "about"
  7. ];
  8. const partialNames = [
  9. "nav"
  10. ];
  11. const loadCount = templateNames.length + partialNames.length;
  12. var templates = {};
  13. var contentElement = document.getElementById("content");
  14. var ready = false;
  15. function checkLoad(target) {
  16. if (Object.keys(templates).length === loadCount) {
  17. ready = true;
  18. target.dispatchEvent(new Event(EVENT_TEMPLATES_LOADED));
  19. }
  20. }
  21. templateNames.forEach(function (name) {
  22. const path = "templates/" + name + ".hbs";
  23. $.get(path, function (data) {
  24. templates[name] = Handlebars.compile(data);
  25. checkLoad(contentElement);
  26. }, "html");
  27. });
  28. partialNames.forEach(function (name) {
  29. const path = "templates/" + name + ".hbs";
  30. $.get(path, function (data) {
  31. templates[name] = Handlebars.compile(data);
  32. Handlebars.registerPartial(name, templates[name]);
  33. checkLoad(contentElement);
  34. }, "html");
  35. });