123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- Router.configure({
- layoutTemplate: 'layout',
- loadingTemplate: 'loading',
- notFoundTemplate: 'notFound',
- waitOn: function () {
- return [
- Meteor.subscribe('notifications')
- ];
- }
- });
- PostsListController = RouteController.extend({
- template: 'postsList',
- increment: 5,
- postsLimit: function () {
- return parseInt(this.params.postsLimit) || this.increment;
- },
- findOptions: function () {
- return {
- sort: this.sort,
- limit: this.postsLimit()
- };
- },
- subscriptions: function () {
- this.postSub = Meteor.subscribe('posts', this.findOptions());
- },
- posts: function () {
- return Posts.find({}, this.findOptions());
- },
- data: function () {
- var hasMore = this.posts().count() === this.postsLimit();
- return {
- posts: this.posts(),
- ready: this.postSub.ready,
- nextPath: hasMore ? this.nextPath() : null
- };
- }
- });
- NewPostsController = PostsListController.extend({
- sort: { submitted: -1, _id: -1 },
- nextPath: function () {
- return Router.routes.newPosts.path({
- postsLimit: this.postsLimit() + this.increment
- });
- }
- });
- BestPostsController = PostsListController.extend({
- sort: { votes: -1, submitted: -1, _id: -1 },
- nextPath: function () {
- return Router.routes.bestPosts.path({
- postsLimit: this.postsLimit() + this.increment
- });
- }
- });
- Router.route('/posts/:_id', {
- name: 'postPage',
- waitOn: function () {
- return [
- Meteor.subscribe('comments', this.params._id),
- Meteor.subscribe('singlePost', this.params._id)
- ];
- },
- data: function () {
- // "this" is the matched route.
- return Posts.findOne(this.params._id);
- }
- });
- Router.route('/posts/:_id/edit', {
- name: 'postEdit',
- waitOn: function () {
- return Meteor.subscribe('singlePost', this.params._id);
- },
- data: function () {
- // "this" is the matched route.
- var ret = Posts.findOne(this.params._id);
- return ret;
- }
- });
- Router.route('/submit', {
- name: 'postSubmit'
- });
- // C'est un nom de route, pas un nom de template. Mais IR le prend comme nom de
- // template par défaut.
- Router.route('/', {
- name: 'home',
- controller: 'NewPostsController',
- });
- Router.route('/new/:postsLimit?', { name: 'newPosts' });
- Router.route('/best/:postsLimit?', { name: 'bestPosts' });
- var requireLogin = function () {
- if (!Meteor.user()) {
- if (Meteor.loggingIn()) {
- // Défini dans Router.configure().
- this.render(this.loadingTemplate);
- } else {
- this.render('accessDenied');
- }
- } else {
- this.next();
- }
- };
- // Faire une 404 si la page matche la route postPage, mais pas son argument.
- // Déclenché pour toute valeur "falsy" (null, false, undefined, empty).
- Router.onBeforeAction('dataNotFound', {
- only: 'postPage'
- });
- // Appliquer le contrôle d'accès à la route postSubmit.
- Router.onBeforeAction(requireLogin, {
- only: 'postSubmit'
- });
|