1

我有消息收集。
每条消息都有一个userId
我还定义了 displayUsername()函数,它获取用户的 id,并返回fullName

我的问题是我可以在服务器上使用 underscorejs 扩展它。或者什么是扩展对象的实用方法

messages = new Meteor.Collection("messages");

Meteor.publish("messages", function () {
  var allMessages = messages.find({}).fetch();
  return _.each(allMessages, function (msg) {
    return _.extend(msg, {
      username: displayName(msg.userId)
    });
});

所以我想要

{{#each messages}}
  <p><strong>{{username}}:</strong> {{messageBody}}</p>
{{/each}}

我知道,这在客户端是可能的,但我将使用它更多时间......

谢谢..

4

2 回答 2

2

检查 Collection.find 上的转换

http://docs.meteor.com/#find

克里斯有一个关于“转换收集文件”的视频 tut 谈话

Meteor Collections 上的转换选项允许我们在 MongoDB 文档在 fetch、findOne 或 find 调用中返回之前以及在它们传递给观察者回调之前转换它们。它为模型层奠定了基础。在这一集中,我将构建一个简单的转换类,该类具有一个 formatPrice 方法,用于在数据库中存储为美分的价格。

http://www.eventedmind.com/posts/meteor-transforming-collection-documents

于 2013-05-02T08:49:56.390 回答
0

不幸的是,您无法发送转换后的集合。但是您可以在客户端对其进行转换。

例如,当您在客户端定义您的收藏时:

客户端js

var messages = new Meteor.Collection("messages", {transform:function(doc) {
    doc.username = displayName(doc.userId);
    return doc;
}});
于 2013-05-02T09:24:20.027 回答