简体   繁体   English

流星路由,发布/订阅

[英]Meteor Routing, Pub/Sub

I'm trying to make a publishment statement to publish ONLY the author(OP)'s profile avatar. 我正在尝试发表发表声明,以仅发表author(OP)的个人资料头像。 I am thinking of grabbing the _id of the page. 我正在考虑获取页面的_id And from that page, I will grab the userId which is the author's _id and try to show the profile. 而从该页面中,我将抓住userId这是笔者的_id ,并尝试以显示个人资料。

However, I have been very unsuccessful, and currently, I am using the following. 但是,我一直做不到成功,目前,我正在使用以下内容。 Publishing EVERY user's profile avatar. 发布每个用户的个人资料头像。

Publications.js Publications.js

//Need to filter this to show only OP.
Meteor.publish("userPostAvatar", function() {
    return Meteor.users.find( {} ,
    {
        fields: {'profile.avatar': 1}
    })
});

Meteor.publish('singlePost', function(id) {
  check(id, String);
  return Posts.find(id);
});


Router.js Router.js

Router.route('/posts/:_id', {
    name: 'postPage',
    waitOn: function() {
        return [
            Meteor.subscribe('singlePost', this.params._id),
            Meteor.subscribe('userStatus'), 
            Meteor.subscribe('userPostAvatar')
        ];
    },
    data: function() { 
        return Posts.findOne({_id:this.params._id});
     }
});

You can do a simple join in the userPostAvatar publish function like this: 您可以在userPostAvatar发布函数中进行如下简单的连接:

Meteor.publish('userPostAvatar', function(postId) {
  check(postId, String);
  var post = Posts.findOne(postId);
  return Meteor.users.find(post.authorId, {fields: {profile: 1}});
});

This assumes posts have an authorId field - adjust as needed for your use case. 假定帖子具有authorId字段-根据您的用例进行调整。 Note three important things: 注意三件事:

  • You will need to subscribe with this.params._id just as you did for singlePost . 您需要像使用singlePost一样订阅this.params._id

  • The join is non-reactive. 联接是非反应性的。 If the author changes, the avatar will not be republished. 如果作者更改,该头像将不会重新发布。 Given the general nature of posts I assume this isn't a problem. 考虑到帖子的一般性质,我认为这不是问题。

  • I didn't publish the nested field profile.avatar on purpose because doing so can cause weird behavior on the client. 我没有故意发布嵌套字段profile.avatar ,因为这样做会导致客户端出现奇怪的行为。 See this question for more details. 有关更多详细信息,请参见此问题

I believe you can achieve this within the iron:router data context, by finding the post, associated author (whatever the field is), and then the subsequent user avatar. 我相信您可以在iron:router数据上下文中实现此目的,方法是查找帖子,关联的作者(无论该字段是什么),然后查找后续的用户头像。 You can return an object to the iron:router data context. 您可以将对象返回到iron:router数据上下文。 Then you can access post and avatar in the template as variables (so you might need to adjust the template output a little). 然后,您可以访问模板中的postavatar作为变量(因此您可能需要稍微调整模板输出)。

Publications.js Publications.js

Meteor.publish("userPostAvatar", function() {
    return Meteor.users.findOne( {} ,
    {
        fields: {'profile.avatar': 1}
    })
});

Meteor.publish('singlePost', function(id) {
  check(id, String);
  return Posts.find(id);
});

Router.js Router.js

Router.route('/posts/:_id', {
    name: 'postPage',
    waitOn: function() {
        return [
            Meteor.subscribe('singlePost', this.params._id),
            Meteor.subscribe('userStatus'), 
            Meteor.subscribe('userPostAvatar')
        ];
    },
    data: function() {
        var post = Posts.findOne({_id: this.params._id});
        var avatar = Users.findOne(post.authorId).profile.avatar;
        return {
            post: post,
            avatar: avatar
        };
    }
});

Two problems with this method are that you could achieve the same thing with template helpers, and the user publication hasn't been limited to one user (I'm unsure how to do this unless we know the authorId within the waitOn, although maybe you could try moving the logic to there instead of the data context as my example shows). 这种方法的两个问题是,您可以使用模板助手来实现相同的目的,并且用户发布并不仅限于一个用户(我不确定如何执行此操作,除非我们知道waitOn中的authorId,尽管也许您可以尝试将逻辑移到那里而不是如我的示例所示的数据上下文)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM