有没有办法在发布函数内的服务器上添加临时额外字段?我似乎无法观察或改造工作.
我有两个订阅相同的集合'列表'.有时我想订阅某些列表,因此它们可用于聊天室列表......但问题是它们出现在我的"列表"模板中.独特的部分是在服务器上的性能(大型阵列).
理想情况下,我希望我可以添加一个额外的字段,例如'forChat:true',这样我就可以在列表模板中检查它,并且只提取没有'forChat'字段的列表.
目前我通过在每个列表中发送'喜欢'和'不喜欢'数组来解决它,因此列表模板可以检查用户的id是否在其中.然而,由于长度〜=(用户/ 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;
});
});
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?有点hacky,但我想我可以将字段添加到每个列表中,并在其余的出版物上省略它.
下面接受的答案的工作代码:
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();
});
Run Code Online (Sandbox Code Playgroud)
是的,这是可能的。请参阅我的答案:How to 'transform' data returned via a Meteor.publish?
它基本上可以归结为meteor文档中发布的扩展示例: http: //docs.meteor.com/#meteor_publish。
| 归档时间: |
|
| 查看次数: |
1720 次 |
| 最近记录: |