router.js 1.8 KB

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