[PLEASE NOTE]: Depreciating this post, instead please see: Patterns and practices for passing data between templates
I’m building a rudimentary hybrid-messaging-forum application. I just started, and one of the basic things that should happen I suppose is, when one clicks on a post within a list of posts, you should go to the post details.
Using this forum as an example, I would click on a post here:
For reference we can call this the Main screen.
And would be taken to a post detail screen here:
For reference the Detail screen
I’ll have two collections published, one for the Main and one for the Detail screens. The Detail screen of course will need a UserId – but not necessarily the UserId the person logged in. Therefore I’ll store the UserId of the person that created the Post in question in the Detail screen’s collection document.
{{#each messages}}
<tr>
<td>{{detail_screen_user_id}}</td>
<td>{{post_description}}</td>
<td>{{last_updated_date}}</td>
</tr>
{{/each}}
This will list out all the users and their posts.
{{#each user_post}}
<tr>
<td>{{post_message}}</td>
<td>{{post_user}}</td>
<td>{{last_updated_date}}</td>
</tr>
{{/each}}
This will list out the details of one post.
I need to be able to pass the {{detail_screen_user_id}} to the Detail-Screen template in order to look up the related posts.
The code for the Detail template looks a lot like this:
Template.post_details.onCreated(function () {
var self = this;
self.autorun(function () {
self.subscribe('user_messages', end_user);
});
});
Template.post_details.helpers({
post_details: function (detail_screen_user_id) {
return user_messages.find({ user_id: detail_screen_user_id }, { sort: { posts: 1 } });
}
});
I would think that the detail_screen_user_id would need to be pass into the onCreated function for the subscription to filter out the user in the detail screen.
The traditional way to pass an argument over from the Main screen to the Detail screen would be to use a route like so:
Router.route(’/posts’, {name: ‘posts.view’, template: ‘posts’});
Router.route(’/post-detail/:_id’, { name: ‘post.detail.view’, template: ‘posts-detail’ });
Then I’d embed the id in a url and when the user clicks it they’ll get the parameter from the url:
{{#each messages}}
<tr>
<td><a href="id_goes_here"{{detail_screen_user_id}}</a></td>
<td>{{post_description}}</td>
<td>{{last_updated_date}}</td>
</tr>
{{/each}}
Is there another, better way to pass the Id I need from the Main to the Detail screen – one that doesn’t involve the router or the url string as parameters? Also, I’d rather not use global reactive variables (Session), but I’m open to local reactive variables (ReactiveVar).