javascript - Meteor:如何在html页面上使用光标数组的数组显示列表

标签 javascript html arrays mongodb meteor

此问题与 this one here 相关,其中我使用成分-配方关系来表示项目和项目组之间的关系。成功插入项目和组后,每个组中的字段之一是项目 id 的数组,问题是如何在关联的 html 页面上列出这些项目和组。

我尝试了下面的JS代码;对于项目,它返回一个简单的光标,对于组,它返回一个光标数组(因为有多个组)的数组(每组有多个项目):

   Recipes = new Mongo.Collection("recipes");
   Ingredients = new Mongo.Collection("ingredients");

   Template.body.helpers({
       ingredients: function() {
           // return 
           var returnedingredients = Ingredients.find({}, {
               sort: {

                   createdAt: -1
               }
           });
           // console.log(returnedingredients);
           return returnedingredients;
       },
       recipes: function() {
           //Show newest recipes at the top
           var itemIds = Recipes.find({}, {
               sort: {
                   createdAt: -1
               },
               // _id: 1
           }).map(function(i) {
               return i.itemIds;
           });
           // return 
           var returnedrecipes = _.map(itemIds, function(oneRecipe) {
               var ingredientsOfRecipe = _.map(oneRecipe, function(itemId) {
                   return Ingredients.find({}, {
                       _Id: itemId
                   });

               });
               // console.log(ingredientsOfRecipe);
               return ingredientsOfRecipe;
           });
           console.log(returnedrecipes);
           return returnedrecipes;
       },
   });

相关的 html 代码。 body 部位:

<h2>recipes</h2>
<ul>
    {{#each recipes}} {{> recipe}} {{/each}}
</ul>
<h2>List of ingredients</h2>
<ul>
    {{#each ingredients}} {{> ingredient}} {{/each}}
</ul>

模板部分:

<template name="ingredient">
    <li class="{{#if checked}}checked{{/if}}">
        <button class="delete">&times;</button>
        <input type="checkbox" checked="{{checked}}" class="toggle-checked" />
        <span class="text">{{ingredientName}}</span>
    </li>
</template>
<template name="recipe">
    <li class="{{#if checked}}checked{{/if}}">
        <button class="delete">&times;</button>
        <input type="checkbox" checked="{{checked}}" class="toggle-checked" />
        <li>
            <ul>
                {{#each this}}
                <li>{{ingredientName}}</li>
                {{/each}}
            </ul>
        </li>
    </li>
</template>

页面正确显示了项目/成分列表。但它无法显示组/食谱列表(或者更确切地说,其目的是显示每个食谱的成分列表)。两个问题: 1.为了在页面上显示菜谱,JS代码返回游标数组的数组是可行的方法吗? 2. 我在处理游标数组的数组时做错了什么?

最佳答案

相关集合可以通过非常简单的方式完成:

html:

<template name="ListOfRecipes">
<h2>Recipes</h2>
  {{#each recipes}}
    {{> recipe}}
    <h3>Ingredients</h3>
    {{#each ingredients}}
      {{> ingredient))
    {{/each}}
  {{/each}}
</template>

js:

Template.listOfRecipes.helpers({
  recipes: function(){
    return Recipes.find({},{sort: {createdAt: -1}});
  },
  ingredients: function(){
    return Ingredients.find({_id: {$in: this.itemIds}},{sort: {createdAt: -1}});
  }
});

ingredients帮助器中this是一个单独的配方对象。

关于javascript - Meteor:如何在html页面上使用光标数组的数组显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33941062/

相关文章:

php - 在 PHP 中使用 MySQL 并尝试将数据库插入表时出现问题

javascript - 使用 JavaScript 的模板字符串

c++ - 动态分配数组和静态数组的区别

javascript - Ajax post不发送数据

javascript - JQuery DataTables 无法识别加载的数据

javascript - 在 javascript 中检测 SNI(服务器名称指示)浏览器支持

javascript - fancybox href ="imageURL"使用 datalist1 中填充的 image1

java - 如何找到一个数组在另一个数组中的出现?并返回第一个数组的索引

javascript - 在 Javascript 中使用 foreach 实现一个数组

javascript - 拖动面板不会降低不透明度