Skip to content

Commit

Permalink
Added routes for post lists, and pages to display them.
Browse files Browse the repository at this point in the history
chapter13-5
  • Loading branch information
tmeasday committed Oct 19, 2015
1 parent de1c910 commit 9014a07
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
8 changes: 7 additions & 1 deletion client/templates/includes/header.html
Expand Up @@ -7,10 +7,16 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{{pathFor 'postsList'}}">Microscope</a>
<a class="navbar-brand" href="{{pathFor 'home'}}">Microscope</a>
</div>
<div class="collapse navbar-collapse" id="navigation">
<ul class="nav navbar-nav">
<li>
<a href="{{pathFor 'newPosts'}}">New</a>
</li>
<li>
<a href="{{pathFor 'bestPosts'}}">Best</a>
</li>
{{#if currentUser}}
<li>
<a href="{{pathFor 'postSubmit'}}">Submit Post</a>
Expand Down
4 changes: 2 additions & 2 deletions client/templates/posts/post_edit.js
Expand Up @@ -42,7 +42,7 @@ Template.postEdit.events({
if (confirm("Delete this post?")) {
var currentPostId = this._id;
Posts.remove(currentPostId);
Router.go('postsList');
Router.go('home');
}
}
});
});
33 changes: 26 additions & 7 deletions lib/router.js
Expand Up @@ -14,7 +14,7 @@ PostsListController = RouteController.extend({
return parseInt(this.params.postsLimit) || this.increment;
},
findOptions: function() {
return {sort: {submitted: -1}, limit: this.postsLimit()};
return {sort: this.sort, limit: this.postsLimit()};
},
subscriptions: function() {
this.postsSub = Meteor.subscribe('posts', this.findOptions());
Expand All @@ -24,15 +24,38 @@ PostsListController = RouteController.extend({
},
data: function() {
var hasMore = this.posts().count() === this.postsLimit();
var nextPath = this.route.path({postsLimit: this.postsLimit() + this.increment});
return {
posts: this.posts(),
ready: this.postsSub.ready,
nextPath: hasMore ? nextPath : null
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('/', {
name: 'home',
controller: NewPostsController
});

Router.route('/new/:postsLimit?', {name: 'newPosts'});

This comment has been minimized.

Copy link
@Macbit

Macbit Jan 31, 2016

controller: NewPostsListController


Router.route('/best/:postsLimit?', {name: 'bestPosts'});

This comment has been minimized.

Copy link
@Macbit

Macbit Jan 31, 2016

controller: BestPostsListController



Router.route('/posts/:_id', {
name: 'postPage',
waitOn: function() {
Expand All @@ -54,10 +77,6 @@ Router.route('/posts/:_id/edit', {

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

Router.route('/:postsLimit?', {
name: 'postsList'
});

var requireLogin = function() {
if (! Meteor.user()) {
if (Meteor.loggingIn()) {
Expand Down

4 comments on commit 9014a07

@Macbit
Copy link

@Macbit Macbit commented on 9014a07 Jan 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these two improvements this code does not work. Controllers have to be set in each case.
Router.route('/new/:postsLimit?', { name: 'newPosts', controller: NewPostsListController });
and
Router.route('/best/:postsLimit?', { name: 'bestPosts', controller: BestPostsListController });

@hassan1709
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My app works perfectly fine without those "improvements" you said.

@Macbit
Copy link

@Macbit Macbit commented on 9014a07 Mar 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hassan, thanks for your comment. I followed the Meteor book and code does not match with the one that is here, although the app should be absolutely the same. In my case I had to make those changes to make it work. So if someone else struggles in the future, he might use this. Hope it makes sense. Im glad it works fine as it is with you :)

@hassan1709
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably my book is up to date!!! Code is exactly as here (till now).
Cheers!

Please sign in to comment.