router.js 2.2 KB

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