routed.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE html>
  2. <!--
  3. Joakim Beng's simple client-side routing example, with John Resig's simple router
  4. @see http://joakim.beng.se/blog/posts/a-javascript-router-in-20-lines.html
  5. @see http://ejohn.org/blog/javascript-micro-templating/
  6. -->
  7. <html>
  8. <head>
  9. <meta charset="utf-8">
  10. <title>Building a router</title>
  11. <script type="text/ecmascript">
  12. // Simple JavaScript Templating
  13. // John Resig - http://ejohn.org/ - MIT Licensed
  14. (function () {
  15. var cache = {};
  16. this.tmpl = function tmpl(str, data) {
  17. // Figure out if we're getting a template, or if we need to
  18. // load the template - and be sure to cache the result.
  19. var fn = !/\W/.test(str) ?
  20. cache[str] = cache[str] ||
  21. tmpl(document.getElementById(str).innerHTML) :
  22. // Generate a reusable function that will serve as a template
  23. // generator (and which will be cached).
  24. new Function("obj",
  25. "var p=[],print=function(){p.push.apply(p,arguments);};" +
  26. // Introduce the data as local variables using with(){}
  27. "with(obj){p.push('" +
  28. // Convert the template into pure JavaScript
  29. str
  30. .replace(/[\r\t\n]/g, " ")
  31. .split("<%").join("\t")
  32. .replace(/((^|%>)[^\t]*)'/g, "$1\r")
  33. .replace(/\t=(.*?)%>/g, "',$1,'")
  34. .split("\t").join("');")
  35. .split("%>").join("p.push('")
  36. .split("\r").join("\\'")
  37. + "');}return p.join('');");
  38. // Provide some basic currying to the user
  39. return data ? fn(data) : fn;
  40. };
  41. })();
  42. </script>
  43. <script type="text/html" id="home">
  44. <h1>Router FTW!</h1>
  45. </script>
  46. <script type="text/html" id="template1">
  47. <h1>Page 1: <%= greeting %></h1>
  48. <p><%= moreText %></p>
  49. </script>
  50. <script type="text/html" id="template2">
  51. <h1>Page 2: <%= heading %></h1>
  52. <p>Lorem ipsum...</p>
  53. </script>
  54. <script type="text/ecmascript" id="routing">
  55. // A hash to store our routes:
  56. var routes = {};
  57. // The route registering function:
  58. function route(path, templateId, controller) {
  59. routes[path] = {
  60. templateId: templateId,
  61. controller: controller
  62. };
  63. }
  64. // Now declare the routes.
  65. route('/', 'home', function () {
  66. });
  67. route('/page1', 'template1', function () {
  68. this.greeting = 'Hello world!';
  69. this.moreText = 'Bacon ipsum...';
  70. });
  71. route('/page2', 'template2', function () {
  72. this.heading = 'I\'m page two!';
  73. });
  74. // The router.
  75. var el = null;
  76. function router () {
  77. // Lazy load view element:
  78. el = el || document.getElementById('view');
  79. // Current route url (getting rid of '#' in hash as well):
  80. var url = location.hash.slice(1) || '/';
  81. // Get route by url:
  82. var route = routes[url];
  83. // Do we have both a view and a route?
  84. if (el && route && route.controller) {
  85. // Render route template with John Resig's template engine:
  86. el.innerHTML = tmpl(route.templateId, new route.controller());
  87. }
  88. }
  89. // Listen on hash change:
  90. window.addEventListener('hashchange', router);
  91. // Listen on page load:
  92. window.addEventListener('load', router);
  93. </script>
  94. </head>
  95. <body>
  96. <ul>
  97. <li><a href="#">Home</a></li>
  98. <li><a href="#/page1">Page 1</a></li>
  99. <li><a href="#/page2">Page 2</a></li>
  100. </ul>
  101. <div id="view"></div>
  102. </body>
  103. </html>