router.js 2.2 KB

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