Skip to content

Commit c298060

Browse files
committedOct 19, 2015
Augmented the postsList route to take a limit
chapter12-2
1 parent fa34863 commit c298060

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed
 

‎client/templates/posts/posts_list.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
Template.postsList.helpers({
2-
posts: function() {
3-
return Posts.find({}, {sort: {submitted: -1}});
4-
}
5-
});

‎lib/router.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ Router.configure({
33
loadingTemplate: 'loading',
44
notFoundTemplate: 'notFound',
55
waitOn: function() {
6-
return [Meteor.subscribe('posts'), Meteor.subscribe('notifications')]
6+
return [Meteor.subscribe('notifications')]
77
}
88
});
99

10-
Router.route('/', {name: 'postsList'});
11-
1210
Router.route('/posts/:_id', {
1311
name: 'postPage',
1412
waitOn: function() {
@@ -24,6 +22,20 @@ Router.route('/posts/:_id/edit', {
2422

2523
Router.route('/submit', {name: 'postSubmit'});
2624

25+
Router.route('/:postsLimit?', {
26+
name: 'postsList',
27+
waitOn: function() {
28+
var limit = parseInt(this.params.postsLimit) || 5;
29+
return Meteor.subscribe('posts', {sort: {submitted: -1}, limit: limit});
30+
},
31+
data: function() {
32+
var limit = parseInt(this.params.postsLimit) || 5;
33+
return {
34+
posts: Posts.find({}, {sort: {submitted: -1}, limit: limit})
35+
};
36+
}
37+
});
38+
2739
var requireLogin = function() {
2840
if (! Meteor.user()) {
2941
if (Meteor.loggingIn()) {

‎server/publications.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
Meteor.publish('posts', function() {
2-
return Posts.find();
1+
Meteor.publish('posts', function(options) {
2+
check(options, {
3+
sort: Object,
4+
limit: Number
5+
});
6+
return Posts.find({}, options);
37
});
48

59
Meteor.publish('comments', function(postId) {

0 commit comments

Comments
 (0)