router.js 2.0 KB

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