router.js 2.3 KB

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