Trying to set up a route for a single post page that performs multiple tasks using iron:router
- Utilizes the template
postPage
- Subscribes to the publications of
singlePost
,userStatus
(displays status and information of the Author of the single post page),comments
. - Retrieves
Comments
documents with the field ofpostId : this.params._id
- Increases Comments List by
Session.get('commentLimit')
Below is the current code implementation.
Router.js
Router.route('/posts/:_id', {
name: 'postPage',
subscriptions: function() {
return [
Meteor.subscribe('singlePost', this.params._id),
Meteor.subscribe('userStatus'),
Meteor.subscribe('comments', {
limit: Number(Session.get('commentLimit'))
})
];
},
data: function() {
return Posts.findOne({_id:this.params._id});
},
});
Publications.js
Meteor.publish('singlePost', function(id) {
check(id, String);
return Posts.find(id);
});
Meteor.publish('comments', function(options) {
check(options, {
limit: Number
});
return Comments.find({}, options);
});
Template.postPage.onCreated
Template.onCreated( function () {
Session.set('commentLimit', 4);
});
Template.postPage.helpers
Template.postPage.helpers({
comments: function () {
var commentCursor = Number(Session.get('commentLimit'));
return Comments.find({postId: this._id}, {limit: commentCursor});
},
});
Template.postPage.events
Template.postPage.events({
'click a.load-more-comments': function (event) {
event.preventDefault();
Session.set('commentLimit', Number(Session.get('commentLimit')) + 4)
}
});
Everything seems to be functioning correctly, but there is one inconsistency that has been identified. The issue I am facing is...
- User navigates to a single post page and adds a comment (works fine).
- User goes to a different single post page and adds a comment (works fine).
- The problem arises here
- The user navigates to a route that is not the single post page.
- User returns to the single post page
- The comments do not appear.
- New comments are added to the database but do not display.
- This issue persists until a
meteor reset
or manual deletion of all comments in MongoDB is performed.
Is there a more effective way to structure my routing and related code to prevent this unexpected behavior?
Any suggestions for better practices?