assignment.js 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. function checkLoad(target) {
  14. if (Object.keys(templates).length === loadCount) {
  15. target.dispatchEvent(new Event(EVENT_TEMPLATES_LOADED));
  16. }
  17. }
  18. templateNames.forEach(function (name) {
  19. const path = "templates/" + name + ".hbs";
  20. $.get(path, function (data) {
  21. templates[name] = Handlebars.compile(data);
  22. checkLoad(contentElement);
  23. }, "html");
  24. });
  25. partialNames.forEach(function (name) {
  26. const path = "templates/" + name + ".hbs";
  27. $.get(path, function (data) {
  28. templates[name] = Handlebars.compile(data);
  29. Handlebars.registerPartial(name, templates[name]);
  30. checkLoad(contentElement);
  31. }, "html");
  32. });