routing.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @file
  3. * Contains the router code and route definitions
  4. *
  5. * Inspired by Joakim Beng"s example.
  6. *
  7. * @see http://joakim.beng.se/blog/posts/a-javascript-router-in-20-lines.html
  8. */
  9. // The routes object.
  10. var routes = {};
  11. // The route registering function.
  12. function route(path, controller) {
  13. routes[path] = controller;
  14. }
  15. /**
  16. * Set the target content to the template result.
  17. *
  18. * Use only if global "ready" is true, otherwise global "templates" may not be
  19. * entirely initialized.
  20. *
  21. * @param {DOMElement} target
  22. * The element in which to inject the template output.
  23. * @param {string} name
  24. * The name of the template to render.
  25. * @param {Object} context
  26. * The template context.
  27. *
  28. * @returns {void}
  29. *
  30. * @globals templates
  31. */
  32. function render(target, name, context) {
  33. var output = null;
  34. var template = templates[name];
  35. if (!template) {
  36. console.error("Template not found", name);
  37. return;
  38. }
  39. output = template(context);
  40. target.innerHTML = output;
  41. }
  42. /**
  43. * The router.
  44. *
  45. * @returns {void}
  46. *
  47. * @globals contentElement, ready, EVENT_TEMPLATES_LOADED
  48. */
  49. function router() {
  50. var context = null;
  51. var CurrentRoute = null;
  52. var listener = null;
  53. var path = null;
  54. var query;
  55. var templateName = null;
  56. var urlArray = null;
  57. // Current route url (getting rid of "#" in hash as well):
  58. var matches = location.hash.match(/^#?([\w]*)(?:\?)?(.*)?/);
  59. path = matches[1] ? matches[1] : "/";
  60. query = matches[2] ? matches[2].split("&").reduce(function (accu, v) {
  61. var vArray = v.split("=");
  62. accu[vArray[0]] = vArray[1];
  63. return accu;
  64. }, {}) : {};
  65. // console.log("url", url, "path", path, "queryObject", queryObject);
  66. // Get route by path:
  67. CurrentRoute = routes[path];
  68. // Do we have a route ?
  69. if (CurrentRoute) {
  70. // Obtain the template context by passing the query to the controller.
  71. context = new CurrentRoute(query);
  72. // Render template with Handlebars
  73. templateName = (path === "/") ? "home" : path;
  74. if (ready) {
  75. console.log("Immediate render");
  76. render(contentElement, templateName, context);
  77. }
  78. else {
  79. console.log("Deferred render");
  80. listener = contentElement.addEventListener(EVENT_TEMPLATES_LOADED, function () {
  81. render(contentElement, templateName, context);
  82. contentElement.removeEventListener(EVENT_TEMPLATES_LOADED, listener);
  83. });
  84. }
  85. }
  86. else {
  87. console.warn("Content found but no route matching", path);
  88. }
  89. }
  90. // Bind router to browser events.
  91. window.addEventListener("hashchange", router);
  92. window.addEventListener("load", router);
  93. // Route declarations.
  94. route("/", function (q) {
  95. console.log("Home controller", q);
  96. });
  97. route("class", function (q) {
  98. console.log("Class controller", q);
  99. this.greeting = "Hello world!";
  100. this.moreText = "Bacon ipsum...";
  101. });
  102. route("species", function (q) {
  103. console.log("Species controller", q);
  104. this.heading = "I\"m page two!";
  105. });