2016-01-04 31 views
1

不能設法制定的路線,以顯示單篇文章與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路由器混淆了......任何幫助? 謝謝

回答

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"}) 
     } 
    }); 

然後,當模板創建您訂閱使用流星的訂閱:

// 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