router.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. var ret = Posts.findOne(this.params._id);
  75. return ret;
  76. }
  77. });
  78. Router.route('/submit', {
  79. name: 'postSubmit'
  80. });
  81. // C'est un nom de route, pas un nom de template. Mais IR le prend comme nom de
  82. // template par défaut.
  83. Router.route('/', {
  84. name: 'home',
  85. controller: 'NewPostsController',
  86. });
  87. Router.route('/new/:postsLimit?', { name: 'newPosts' });
  88. Router.route('/best/:postsLimit?', { name: 'bestPosts' });
  89. var requireLogin = function () {
  90. if (!Meteor.user()) {
  91. if (Meteor.loggingIn()) {
  92. // Défini dans Router.configure().
  93. this.render(this.loadingTemplate);
  94. } else {
  95. this.render('accessDenied');
  96. }
  97. } else {
  98. this.next();
  99. }
  100. };
  101. // Faire une 404 si la page matche la route postPage, mais pas son argument.
  102. // Déclenché pour toute valeur "falsy" (null, false, undefined, empty).
  103. Router.onBeforeAction('dataNotFound', {
  104. only: 'postPage'
  105. });
  106. // Appliquer le contrôle d'accès à la route postSubmit.
  107. Router.onBeforeAction(requireLogin, {
  108. only: 'postSubmit'
  109. });