Meteor:使用模板中子文档中的数组元素 [英] Meteor: Use an array element from a sub-document in template

查看:38
本文介绍了Meteor:使用模板中子文档中的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 Mongo 文档:

I have a Mongo document like this:

{
    stKey: "apples001",
    i: [        
        {q: "What are apples?", a1: "Apples are fruits.", a2: "Fruit."},
        {q: "How do apples taste?", a1: "Apples taste sweet.", a2: "Sweet."} 
    ]
} 

... 我希望能够在我的模板中使用上面 i: 的内容.对于我的路线,我有这个:

... and I want to be able to use the contents of i: above in my template. For my route, I have this:

  this.route('teacherCue', { 
  path: '/',
  data: function() {
      templateData = { interactionSet: PL.find( 
          {stKey: 'apples001'}
      )};
      return templateData;
  }
});

我的模板如下所示:

<template name="teacherCue">

    {{#each interactionSet}}

      <a href="#">{{interaction.q}}</a>
        <ul>        
            <p><a href="#">{{interaction.a1}}</a> </p> 
            <p><a href="#">{{interaction.a2}}</a> </p>   
        </ul>

    {{/each}}

</template>

所以问题是:如何创建模板助手(或 Handlebars 助手),以便 {{interaction.q}} 中的interaction"等引用字段i"中的特定数组元素?或者有没有其他方法可以达到同样的结果?在这一点上,我不在乎是否可以将参数传递给帮助程序以获取它.如果需要,可以从会话变量中获取数组中的索引.

So the question is: how do I create a template helper (or a Handlebars helper) so that 'interaction' in {{interaction.q}}, etc. references a specific array element from the field 'i'? Or is there another way to achieve this same result? At this point, I don't care if I can pass a parameter to the helper to get it or not. The index into the array can be gotten from a session variable, if I have to.

推荐答案

好的,这个问题费了好大劲才解决,可能还有更简单的方法,但是这里有一个方法可以返回特定的数组元素从子文档作为模板数据.最初,我试图使用 #each 为模板建立数据上下文,但这只是令人困惑的事情,b/c 我不想遍历结果集,我想为我的文件使用单个文档数据.具体来说,我想使用一个对象,该对象是该文档中对象数组中的一个元素;并将该对象的不同值分配给模板值,例如 {{myValue1}}.

Ok, this one took a lot of tinkering to solve, and there may well be a simpler way, but here is a method that works to return specific array elements from a sub-document as template data. Originally, I was trying to use an #each to establish the data context for the template, but that was just confusing things, b/c I don't want to iterate through a result set, I want to use a single document for my data. Specifically I want to use an object that is an element in an object array in that document; and assign different values from that object to template values, like {{myValue1}}.

所以我必须做的是使用 jQuery(它给我一个对象数组)将我的对象数组字段转换为真实数组",然后访问该数组中的正确元素并转换成数组!最后,要访问所需对象中的各个值对,我必须使用类似 myArray[0].fieldName 的语法.

So what I had to to do was turn my object array field into a "real array" using jQuery (which gave me an array of objects) and then access the right element in that array and turn it into an array! Finally, to access the individual value pairs in the desired object, I had to use a syntax like myArray[0].fieldName.

this.route('teacherCue', {

  path: '/',

  data: function() {

      desiredIndex = 0;

      var record = PL.findOne( {stKey: 'apples001'} );

      var objArray = $.makeArray( record.i );

      iArray = $.makeArray( objArray[desiredIndex] );

      return {q: iArray[0].q, a1: iArray[0].a1, a2: iArray[0].a2, q2: iArray[0].q2}
  }
});

感谢肖恩的回答为我指明了正确的方向.

Thanks to Sean for pointing me in the right direction with his answer.

这篇关于Meteor:使用模板中子文档中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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