流星流量路由器设置site.com/singlePostPage

问题描述:

不能设法制定的路线,以显示单篇文章与flowrouter和流星火焰..流星流量路由器设置site.com/singlePostPage

这是我到目前为止,我敢肯定它的大多是错的!

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路由器混淆了......任何帮助? 谢谢

首先不要使用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"}) 
     } 
    }); 

然后,当模板创建您订阅使用流星的订阅:

// 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}} 
+0

谢谢,但仍然不会工作...如何发布功能知道什么是postId?我没有把它定义在任何地方......我实际上是从其他地方复制代码的一部分,使其工作,但我认为这是我最困惑的部分...谢谢 – Adam

+0

挂起,我正在编辑我的评论,因为我注意到其他的事情。别担心,我也有这个问题。稍等一会儿;) – mesosteros

+0

查看更新。 – mesosteros