我怎样才能添加temp。字段到Meteor发布 [英] How can I add temp. fields to a Meteor publish

查看:68
本文介绍了我怎样才能添加temp。字段到Meteor发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在发布函数内的服务器上添加临时额外字段?我似乎无法观察或改造工作。

Is there any way to add an temp extra field on the server inside of a publish function? I can't seem to get observe or transform to work.

我有两个订阅相同的集合'列表'。有时我想订阅某些列表,因此它们可用于聊天室列表......但问题是它们出现在我的列表模板中。唯一的部分是服务器上的性能(大型数组)。

I have two subscriptions for the same collection 'listings'. There are times when I want to subscribe to certain listings so they're available for the chatroom list... but the problem is they're showing up in my 'listings' template. The unique part was on the server for performance (large array).

理想情况下,我希望我可以添加一个额外的字段,如'forChat:true',以便我可以检查在列表模板中,只有拉出没有'forChat'字段的列表。

Ideally I wish I could add an extra field like 'forChat:true' so that I can check for that in the listings template and only pull in listings that dont have a 'forChat' field.

目前我正在通过发送'喜欢'和&每个列表中的'disliked'数组,因此列表模板可以检查用户的id是否在其中。然而,由于长度〜=(用户/ 2),这将无法随时间(以及在移动设备上)很好地扩展。

Currently i'm getting around it by sending down the 'liked' & 'disliked' array in each listing so the listings template can check if the user's id is inside of it. However this won't scale well with time (and on mobile) due to the length ~= (users / 2).

// ideal-ish pseudo code if we could return arrays:

Meteor.publish('chats', function(id) {
    lists = Listings.find(...).fetch();

    return lists.map(function(list){
        return list.forChat = true;
    });
});

这甚至可能吗?有点hacky,但我想我可以将字段添加到每个列表中,并在其余的出版物上省略它。

Is this even possible? Kind of hacky, but I suppose I could add the field to every listing and omit it on the rest of the publications.

下面接受的答案中的工作代码:

Working code from accepted answer below:

Meteor.publish('listingsForChats', function(id) {
    var cursor = Listings.find(...);

    // insert a temp `forChats:true` field to filter in listings template
    cursor.forEach(function(doc) {
      doc.forChats = true;
      this.added('listings', doc._id, doc);
    }, this);

    this.ready(); 
});


推荐答案

是的,这是可能的。请在此处查看我的回答:如何'转换'通过Meteor.publish返回的数据?

Yes, that is possible. See my answer here: How to 'transform' data returned via a Meteor.publish?

它基本上归结为流星文档中发布的扩展示例: http://docs.meteor.com/#meteor_publish

It basically comes down to the extended example of publish in the meteor documentation: http://docs.meteor.com/#meteor_publish.

这篇关于我怎样才能添加temp。字段到Meteor发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆