routing.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!--
  2. @license
  3. Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
  4. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  5. The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  6. The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  7. Code distributed by Google as part of the polymer project is also
  8. subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  9. -->
  10. <script src="../bower_components/page/page.js"></script>
  11. <script>
  12. window.addEventListener('WebComponentsReady', function() {
  13. // We use Page.js for routing. This is a Micro
  14. // client-side router inspired by the Express router
  15. // More info: https://visionmedia.github.io/page.js/
  16. // Removes end / from app.baseUrl which page.base requires for production
  17. if (window.location.port === '') { // if production
  18. page.base(app.baseUrl.replace(/\/$/, ''));
  19. }
  20. // Middleware
  21. function scrollToTop(ctx, next) {
  22. app.scrollPageToTop();
  23. next();
  24. }
  25. function closeDrawer(ctx, next) {
  26. app.closeDrawer();
  27. next();
  28. }
  29. // Routes
  30. page('*', scrollToTop, closeDrawer, function(ctx, next) {
  31. next();
  32. });
  33. page('/', function() {
  34. app.route = 'home';
  35. });
  36. page(app.baseUrl, function() {
  37. app.route = 'home';
  38. });
  39. page('/users', function() {
  40. app.route = 'users';
  41. });
  42. page('/users/:name', function(data) {
  43. app.route = 'user-info';
  44. app.params = data.params;
  45. });
  46. page('/contact', function() {
  47. app.route = 'contact';
  48. });
  49. // 404
  50. page('*', function() {
  51. app.$.toast.text = 'Can\'t find: ' + window.location.href + '. Redirected you to Home Page';
  52. app.$.toast.show();
  53. page.redirect(app.baseUrl);
  54. });
  55. // add #! before urls
  56. page({
  57. hashbang: true
  58. });
  59. });
  60. </script>