assignment.js 960 B

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