Guest User

Untitled

a guest
Feb 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. //Add a namspace
  2. window.MyWebApp = {};
  3.  
  4.  
  5. var Routes = {
  6. Home: {
  7. init: function () {
  8. // controller-wide code
  9. },
  10. NewJsPage: function () {
  11. // action-specific code
  12. import("./newJsPage").then((module) => {
  13. window.MyWebApp.newJsPage = module.newJsPage;
  14. });
  15. },
  16. Privacy: function () {
  17. // Privacy action code
  18. }
  19. }
  20. };
  21.  
  22. var Router = {
  23. exec: function (controller, action) {
  24. action = action === undefined ? "init" : action;
  25.  
  26. if (controller !== "" && Routes[controller] && typeof Routes[controller][action] === "function") {
  27. Routes[controller][action]();
  28. }
  29. },
  30.  
  31. init: function () {
  32. let body = document.body;
  33. let controller = body.getAttribute("data-controller");
  34. let action = body.getAttribute("data-action");
  35.  
  36. Router.exec(controller);
  37. Router.exec(controller, action);
  38.  
  39. }
  40. };
  41.  
  42. $(function () {
  43. //run this when the DOM is ready
  44. Router.init();
  45. });
Add Comment
Please, Sign In to add comment