我无法设法在Meteor中创建使用flowrouter和fireze显示单个帖子的路由。
这是我到目前为止所确定的大多数错误!
publications.js
Meteor.publish('singlePost', function (postId) {
return Posts.find({ _id: postId });
});
Router.js
FlowRouter.route("/posts/:_id", {
name: "postPage",
subscriptions: function (params, queryParams) {
this.register('postPage', Meteor.subscribe('singlePost'));
},
action: function(params, queryParams) {
BlazeLayout.render("nav", {yield: "postPage"} )
}
});
singlePost.JS
Template.postPage.helpers({
thisPost: function(){
return Posts.findOne();
}
});
singlePost.html
<template name="postPage">
{{#with thisPost}}
<li>{{title}}</li>
{{/with}}
</template>
我曾经使用Iron路由器做过,但现在却与Flow路由器混淆了。
答案 0 :(得分:1)
首先不要使用FlowRouter订阅。这很快就会被弃用。使用Meteor PubSub。首先在routes.js:
// http://app.com/posts/:_id
FlowRouter.route('/posts/:id', {
name: "postPage",
action: function(params, queryParams) {
BlazeLayout.render("nav", {yield: "postPage"} )
}
});
然后在创建模板时,您使用Meteor的订阅订阅:
// Template onCreated
Template.postPage.onCreated(function() {
// Subscribe only the relevant subscription to this page
var self = this;
self.autorun(function() { // Stops all current subscriptions
var id = FlowRouter.getParam('id'); // Get the collection id from the route parameter
self.subscribe('singlePost', id); // Subscribe to the single entry in the collection with the route params id
});
});
然后帮手将是:
// Template helper functions
Template.postPage.helpers({
thisPost: function() {
// Get the single entry from the collection with the route params id
var id = FlowRouter.getParam('id');
var post = Posts.findOne({ // Get the selected entry data from the collection with the given id.
_id: id
}) || {};
return post;
}
});
您还需要检查订阅是否已在html中准备好。
{{#if Template.subscriptionsReady}}
{{#with thisPost}}
<li>{{title}}</li>
{{/with}}
{{else}}
<p>nothing to show</p>
{{/if}}