Skip to content
Permalink
Browse files

Added routes for post lists, and pages to display them.

chapter13-5
  • Loading branch information
tmeasday committed Oct 19, 2015
1 parent de1c910 commit 9014a0708fc2276fccf12aaeac56784088030bde
Showing with 35 additions and 10 deletions.
  1. +7 −1 client/templates/includes/header.html
  2. +2 −2 client/templates/posts/post_edit.js
  3. +26 −7 lib/router.js
@@ -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>
@@ -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');
}
}
});
});
@@ -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());
@@ -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() {
@@ -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()) {

4 comments on commit 9014a07

@Macbit

This comment has been minimized.

Copy link

@Macbit Macbit replied Jan 31, 2016

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

This comment has been minimized.

Copy link

@hassan1709 hassan1709 replied Mar 12, 2016

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

@Macbit

This comment has been minimized.

Copy link

@Macbit Macbit replied Mar 12, 2016

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

This comment has been minimized.

Copy link

@hassan1709 hassan1709 replied Mar 12, 2016

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

Please sign in to comment.
You can’t perform that action at this time.