Kaynağa Gözat

Commit 12-2: Augmented the postsList route to take a limit.

Frederic G. MARAND 9 yıl önce
ebeveyn
işleme
b7d4cb3d2f
3 değiştirilmiş dosya ile 34 ekleme ve 15 silme
  1. 0 5
      client/templates/posts/posts_list.js
  2. 26 7
      lib/router.js
  3. 8 3
      server/publications.js

+ 0 - 5
client/templates/posts/posts_list.js

@@ -1,5 +0,0 @@
-Template.postsList.helpers({
-  posts: function () {
-    return Posts.find({}, { sort: { submitted: -1 }});
-  }
-});

+ 26 - 7
lib/router.js

@@ -4,18 +4,11 @@ Router.configure({
   notFoundTemplate: 'notFound',
   waitOn: function () {
     return [
-      Meteor.subscribe('posts'),
       Meteor.subscribe('notifications')
     ];
   }
 });
 
-// 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: 'postsList'
-});
-
 Router.route('/posts/:_id', {
   name: 'postPage',
   waitOn: function () {
@@ -29,6 +22,9 @@ Router.route('/posts/:_id', {
 
 Router.route('/posts/:_id/edit', {
   name: 'postEdit',
+  waitOn: function () {
+    return Meteor.subscribe('singlePost', this.params._id);
+  },
   data: function () {
     // "this" is the matched route.
     return Posts.findOne(this.params._id);
@@ -39,6 +35,29 @@ 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('/:postLimit?', {
+  name: 'postsList',
+  waitOn: function () {
+    var limit = parseInt(this.params.postLimit) || 5;
+    return Meteor.subscribe('posts', {
+      sort: { submitted: -1 },
+      limit: limit
+    });
+  },
+  data: function () {
+    var limit = parseInt(this.params.postLimit) || 5;
+    return {
+      posts: Posts.find({}, {
+        sort: { submitted: -1 },
+        limit: limit
+      })
+    };
+  }
+});
+
+
 var requireLogin = function () {
   if (!Meteor.user()) {
     if (Meteor.loggingIn()) {

+ 8 - 3
server/publications.js

@@ -1,8 +1,13 @@
-Meteor.publish('posts', function() {
-  return Posts.find();
+Meteor.publish('posts', function (options) {
+  // It would probably be safe to pass sort and limit as 2 separate params.
+  check(options, {
+    sort: Object,
+    limit: Number
+  });
+  return Posts.find({}, options);
 });
 
-Meteor.publish('comments', function(postId) {
+Meteor.publish('comments', function (postId) {
   check(postId, String);
   return Comments.find({
     postId: postId