使用 Meteor 渲染 MongoDB 文档 [英] Rendering MongoDB documents with Meteor

查看:33
本文介绍了使用 Meteor 渲染 MongoDB 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:如何显示从Mongodb返回的复杂文档.

The question is: how to display complex documents returned from Mongodb.

假设我的 Projects 集合中有两个项目,每个项目都有 2 个 People,这些 People 嵌入了 People 集合中的 ID.Project.find() 返回此 CURSOR,我将其显示为 JSON 以提高可读性:

Suppose I have two projects in my Projects collection and each project has 2 People embedded by their IDs from the People collection. Project.find() returns this CURSOR which I'm presenting as JSON for readability:

{ "_id" : "5ed5ade8-c140-46f7-8d9e-2344fc53df8e",  "name" : "Project1", "personnel" : [ "b4690c4b-d126-4737-8f40-921234567890", "977a6335-a1be-4af5-8b3f-4abcdefghijk" ] }
{ "_id" : "23f073c7-a713-4713-86a7-7d6600433146", "name" : "Project2", "personnel" : [ "b4690c4b-d126-4737-8f40-92b43072d7a1", "977a6335-a1be-4af5-8b3f-48cd5f8328db" ]}

我在 .html 文件中的模板:

My template in .html file:

<template name="theProjects">
     {{#each theTitles}}
          Name: {{name}}
          Personnel: {{personnel}}
     {{/each}}
</template>

在浏览器中渲染:

名称:项目 1

人员:b4690c4b-d126-4737-8f40-921234567890,977a6335-a1be-4af5-8b3f-4abcdefghijk

Personnel: b4690c4b-d126-4737-8f40-921234567890,977a6335-a1be-4af5-8b3f-4abcdefghijk

名称:Project2

Name: Project2

人员:b4690c4b-d126-4737-8f40-92b43072d7a1,977a6335-a1be-4af5-8b3f-48cd5f8328db

Personnel:b4690c4b-d126-4737-8f40-92b43072d7a1,977a6335-a1be-4af5-8b3f-48cd5f8328db

问题:

  1. {{personnel}} 字段只是由 Projects 集合中人员数组的内容填充.我想在他们自己的行上单独列出这些.想不通.只是切片不行,因为...

  1. The {{personnel}} field is simply being filled in by the contents of the personnel array in the Projects collect. I want to list these separately on their own line. Can't figure that out. Just slicing won't do because...

显然更大的需求是能够操作和编辑人员数据,因此模板必须能够正确响应 Meteor.

The greater need clearly is to be able to manipulate and edit the Personnel data so the template has to be properly Meteor reactive.

现在是困难的一个:来自 People 集合的人员 ID 被嵌入到项目文档中.我将如何使用 Meteor 将 ID 替换为 Personnel 集合中的适当名称,同时仍保持其数据反应性?

Now the hard one: the personnel ID's from the People collection are embedded in the Project documents. How would I use Meteor to replace the ID's with the appropriate names from the Personnel collection and still keep their data reactivity?

我知道有很多问题要问,但这些事情似乎是更大、更复杂网站的基础.

I know this is a lot to ask but these kind of things seem like they are foundational to bigger and more complex web site.

谢谢大家.

推荐答案

https://gist.github.com/3220991

这是我向您展示使用 Handlebars 助手的要点.这将保持反应性并且非常易于使用.希望你能用上.

This is a gist I made to show you use the of Handlebars helpers. This will stay reactive and is very easy to use. I hope you can use it.

如果您需要更多帮助或评论,请提及我.

If you need more help or comments , just mention me.

这篇关于使用 Meteor 渲染 MongoDB 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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