1

我无法创建路线以在 Meteor 中使用 flowrouter 和 blaze 显示单个帖子。

这是我到目前为止所拥有的,我相信它大部分是错误的!

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 路由器感到困惑。

4

1 回答 1

1

首先不要使用 FlowRouter 订阅。这将很快被弃用。使用流星 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}}
于 2016-01-04T01:13:25.473 回答