assignment.js 1.0 KB

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