router.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @file
  3. *
  4. *
  5. * User: marand
  6. * Date: 30/08/15
  7. * Time: 16:47
  8. */
  9. Router.configure({
  10. layoutTemplate: "layout",
  11. loadingTemplate: "loading",
  12. notFoundTemplate: "notFound",
  13. waitOn: function () {
  14. return [
  15. Meteor.subscribe('posts'),
  16. Meteor.subscribe('notifications')
  17. ];
  18. }
  19. });
  20. // C'est un nom de route, pas un nom de template. Mais IR le prend comme nom de
  21. // template par défaut.
  22. Router.route('/', {
  23. name: 'postsList'
  24. });
  25. Router.route('/posts/:_id', {
  26. name: 'postPage',
  27. waitOn: function () {
  28. return Meteor.subscribe('comments', this.params._id);
  29. },
  30. data: function () {
  31. // "this" is the matched route.
  32. return Posts.findOne(this.params._id);
  33. }
  34. });
  35. Router.route('/posts/:_id/edit', {
  36. name: 'postEdit',
  37. data: function () {
  38. // "this" is the matched route.
  39. return Posts.findOne(this.params._id);
  40. }
  41. });
  42. Router.route('/submit', {
  43. name: 'postSubmit'
  44. });
  45. // Faire une 404 si la page matche la route postPage, mais pas son argument.
  46. // Déclenché pour toute valeur "falsy" (null, false, undefined, empty).
  47. Router.onBeforeAction('dataNotFound', {
  48. only: 'postPage'
  49. });
  50. var requireLogin = function () {
  51. if (!Meteor.user()) {
  52. if (Meteor.loggingIn()) {
  53. // Défini dans Router.configure().
  54. this.render(this.loadingTemplate);
  55. }
  56. else {
  57. this.render('accessDenied');
  58. }
  59. }
  60. else {
  61. this.next();
  62. }
  63. };
  64. // Appliquer le contrôle d'accès à la route postSubmit.
  65. Router.onBeforeAction(requireLogin, {
  66. only: 'postSubmit'
  67. });