Javascript Meteor:在使用模板之前修改Mongo查询结果的最佳实践?

Javascript Meteor:在使用模板之前修改Mongo查询结果的最佳实践?,javascript,mongodb,templates,meteor,conceptual,Javascript,Mongodb,Templates,Meteor,Conceptual,学习流星,我仍然陷入概念的泥潭 我有一个集合,其中每个项目都有一个名为“props”的数组,其中包含零个或多个属性名:loudmouth、fixer、networker、outcast等 我还有一个属性对象集合,每个属性对象都与一个glyph图标关联 现在,我的模板将数据项显示为包含属性标识符的行的无序列表。我想用相应的图标HTML替换属性标识符 {name: "loudmouth", icon: "bullhorn", other: "rtgb"} {name: "fixer", icon:

学习流星,我仍然陷入概念的泥潭

我有一个集合,其中每个项目都有一个名为“props”的数组,其中包含零个或多个属性名:loudmouth、fixer、networker、outcast等

我还有一个属性对象集合,每个属性对象都与一个glyph图标关联

现在,我的模板将数据项显示为包含属性标识符的
  • 行的无序列表。我想用相应的图标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>
    
  • AAA扩音器固定器弃儿
  • 固定器
  • 。。。应该变成这个

    <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>
    
  • AAA
  • BBB
  • 这不是SQL连接;我正在为每个返回的结果项添加属性。最好的方法是什么

    我试过几种方法,但都弄得一团糟

    1) 第一个是客户机中一个粗糙的内部/外部循环作业。对于外部循环中的每个数据项,我将附加从内部循环的字符串连接起来的标记。这似乎给了我某种比赛条件,带有虚假的“未定义”s。(我还不清楚它的同步性规则)。我是否应该将properties集合预取到名称/值映射对象中?而且,奇怪的是,我的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>
    
    {{> glyph 'fixer'}}
    
    或者,如果参数来自迭代器对象

    {{> glyph name}}
    
    (假设对象具有
    .name
    属性)

    在任何情况下,我建议你们重读《流星》中的帮助者和反应性,并进行实验。因为客户机中已经有了集合,所以通常只需要一个helper函数就可以解决这类问题。Meteor(re)会随着收藏的更新而计算,因此您永远不必担心它。

    请检查我的理解: 1个用户有1个属性。 1属性有1个
    名称
    ,1个
    图标
    ,1个
    其他

    如果是这种情况,请在门口检查您的3NF&只需将它们嵌入到用户文档中即可。因此,您的模式可能如下所示:

    {userId: 'asldkfjz', 
     userName: 'Jonny', 
     properties: {
       name: "loudmouth", 
       icon: "bullhorn", 
       other: "rtgb"
     }
    }
    
    使用MongoDB,您始终需要权衡空间成本和访问成本。存储1个外键+一个额外订阅可能(最肯定)比存储3个字符串更昂贵,对吗

    接下来,我将介绍如何设置您的模板:

    {{#each user}}
        {{>formatRow icon=properties.icon name=properties.name other=properties.other}}
    {{/each}}
    
    <template name="formatRow">
      <div class="blah">
        <li>{{name}} 
            <span class="glyphicon {{icon}}" aria-hidden="true"></span> 
        </li>
      </div>
    </template>
    
    了解更多信息,请访问:


    抱歉,但是,一个用户作为属性数组:
  • AAA loudmouth fixer outcast
  • 您正在收藏中存储HTML字符串?这是完全可能的,但不会影响我试图解决的概念问题。B.t.w我非常感谢您回答的细节,但真正的问题是,在从后端到前端的数据流中,用图标名替换零个或多个属性名的正确方法。但是,为什么{{icon this}}而不是{{{icon}}?所以,事实上,我必须这样做:
  • {{{fullName}{{{each prop}}{{>glyph name}{{/each}
  • 。是吗?是的,你可以做
    {{icon}}
    ,因为模板上下文(“数据”)会传递到嵌入的模板中,我忘记了这一点。是的,如果存在
    prop.name
    ,它应该可以正常工作。
    {{#each user}}
        {{>formatRow icon=properties.icon name=properties.name other=properties.other}}
    {{/each}}
    
    <template name="formatRow">
      <div class="blah">
        <li>{{name}} 
            <span class="glyphicon {{icon}}" aria-hidden="true"></span> 
        </li>
      </div>
    </template>
    
    Template.foo.helpers({
      user: function () {
        return users.find();
      }
    });