学习Meteor,我还是陷在概念泥潭里。
我有一个集合,其中每个项目都有一个名为“props”的数组,该数组包含零个或多个属性名称:loudmouth、fixer、networker、outcast 等。
我还有一个属性对象集合,每个属性对象都与一个字形图标相关联。
现在,我的模板将数据项显示为包含属性标识符的
行无序列表。我想用相应的图标 HTML 替换属性标识符{name: "loudmouth", icon: "bullhorn", other: "rtgb"}
{name: "fixer", icon: "wrench", other: "iun"}
{name: "networker", icon: "link", other: "retf"}
{name: "outcast", icon: "ban-circle", other: "vcfdds"}
所以这个...
<li>AAA loudmouth fixer outcast</li>
<li>BBB fixer networker</li>
...应该变成这样...
<li>AAA <span class="glyphicon glyphicon-bullhorn" aria-hidden="true"></span> <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> <span class="glyphicon glyphicon-ban-circle" aria-hidden="true"></span></li>
<li>BBB <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> <span class="glyphicon glyphicon-link" aria-hidden="true"></span></li>
这不是 SQL 连接;我正在向每个返回的结果项添加属性。最好的方法是什么?
我尝试了几种方法,但它们真的很困惑。
1) 第一个是客户端中的粗略内/外循环作业。对于外部中的每个数据项,我会附加由内部循环中的字符串连接而成的 标记。这似乎给了我某种竞争条件,带有虚假的“未定义”。 (我还不清楚它的同步性规则)。我应该将属性集合预取到名称/值映射对象中吗?另外,奇怪的是,我的 HTML 显示了 标签的文本而不是图标?!?!啊?
2) 然后我尝试使用 Meteor.method 和 Meteor.call 在服务器端执行此操作。我无法同步获取数据,并且并不真正理解如何异步填充模板(从回调)。您是否在 Template.mine.created() 期间填充数组,然后从 Template.mine.helper 返回该数组? Meteor.method/Meteor.call 组合应该用于更改后端数据,而不是用于检索后端数据,我对吗?
有人知道关于此类事情的好教程吗?你会怎么做?
我希望这不会因为“基于意见”而被拒绝,我相信所有有效的替代方案都会帮助其他有相关问题需要解决的人。
最佳答案
您可以使用参数化模板来完成此操作:
<template name="glyph">
<span class="glyphicon glyphicon-{{icon this}}" aria-hidden="true"></span>
</template>
添加一个辅助函数,例如:
Template.glyph.helpers({
icon: function(name) {
return <Iconcollection>.findOne({name:name}).icon
}
});
然后您可以在模板中执行类似的操作:
{{> glyph 'fixer'}}
或者,如果参数来自迭代器对象
{{> glyph name}}
(假设您的对象具有 .name
属性)
无论如何,我建议您重新阅读并尝试 Meteor 中的助手和 react 性。由于您已经在客户端中拥有该集合,因此通常只需使用辅助函数即可解决此类问题。 Meteor 在集合更新时(重新)计算,因此您永远不必担心它。
关于javascript - meteor : best practice for modifying Mongo query result before template takes it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28813103/