router.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: this.sort,
  20. limit: this.postsLimit()
  21. };
  22. },
  23. subscriptions: function () {
  24. this.postSub = Meteor.subscribe('posts', this.findOptions());
  25. },
  26. posts: function () {
  27. return Posts.find({}, this.findOptions());
  28. },
  29. data: function () {
  30. var hasMore = this.posts().count() === this.postsLimit();
  31. return {
  32. posts: this.posts(),
  33. ready: this.postSub.ready,
  34. nextPath: hasMore ? this.nextPath() : null
  35. };
  36. }
  37. });
  38. NewPostsController = PostsListController.extend({
  39. sort: { submitted: -1, _id: -1 },
  40. nextPath: function () {
  41. return Router.routes.newPosts.path({
  42. postsLimit: this.postsLimit() + this.increment
  43. });
  44. }
  45. });
  46. BestPostsController = PostsListController.extend({
  47. sort: { votes: -1, submitted: -1, _id: -1 },
  48. nextPath: function () {
  49. return Router.routes.bestPosts.path({
  50. postsLimit: this.postsLimit() + this.increment
  51. });
  52. }
  53. });
  54. Router.route('/posts/:_id', {
  55. name: 'postPage',
  56. waitOn: function () {
  57. return [
  58. Meteor.subscribe('comments', this.params._id),
  59. Meteor.subscribe('singlePost', this.params._id)
  60. ];
  61. },
  62. data: function () {
  63. // "this" is the matched route.
  64. return Posts.findOne(this.params._id);
  65. }
  66. });
  67. Router.route('/posts/:_id/edit', {
  68. name: 'postEdit',
  69. waitOn: function () {
  70. return Meteor.subscribe('singlePost', this.params._id);
  71. },
  72. data: function () {
  73. // "this" is the matched route.
  74. return Posts.findOne(this.params._id);
  75. }
  76. });
  77. Router.route('/submit', {
  78. name: 'postSubmit'
  79. });
  80. // C'est un nom de route, pas un nom de template. Mais IR le prend comme nom de
  81. // template par défaut.
  82. Router.route('/', {
  83. name: 'home',
  84. controller: 'NewPostsController',
  85. });
  86. Router.route('/new/:postsLimit?', { name: 'newPosts' });
  87. Router.route('/best/:postsLimit?', { name: 'bestPosts' });
  88. var requireLogin = function () {
  89. if (!Meteor.user()) {
  90. if (Meteor.loggingIn()) {
  91. // Défini dans Router.configure().
  92. this.render(this.loadingTemplate);
  93. } else {
  94. this.render('accessDenied');
  95. }
  96. } else {
  97. this.next();
  98. }
  99. };
  100. // Faire une 404 si la page matche la route postPage, mais pas son argument.
  101. // Déclenché pour toute valeur "falsy" (null, false, undefined, empty).
  102. Router.onBeforeAction('dataNotFound', {
  103. only: 'postPage'
  104. });
  105. // Appliquer le contrôle d'accès à la route postSubmit.
  106. Router.onBeforeAction(requireLogin, {
  107. only: 'postSubmit'
  108. });