概述:
Meteor模板中未渲染父类别中嵌套的子类别。
详情:
考虑一个“类别(Category)”的数据模型:
// Model Schema
Category {
idCategory : 20, (id of the category itself)
idCategoryParent : 0, (idCategory of our parent category)
defaultLabel : "Movies" (our label)
}
有父级分类和子级分类。父级分类的idCategoryParent属性值为0。子级分类将其父级的idCategory作为其idCategoryParent属性存储。我正在尝试循环遍历这些分类的集合,并以以下方式呈现它们:
<b>Movies</b> // parent category is in bold
<ul> // child categories are rendered as an unordered list
<li>Horror</li>
<li>Comedy</li>
<li>Action</li>
<li>Drama</li>
</ul>
<b>Music</b>
<ul>
<li>Rock</li>
<li>Classical</li>
<li>Ambient</li>
</ul>
然而,实际得到的是:
<b>Movies</b>
<ul> // empty...
</ul>
<b>Music</b>
<ul>
</ul>
源代码:
// How we get the 'categories_parents' data
Template.content.categories_parents = function (){
/*
* Get all parent categories (categories with an idCategoryParent of 0)
*/
var parents = Categories.find({idCategoryParent:0});
var pCount = parents.count();
for (var i = 0; i < pCount; i++){
var pId = parents.db_objects[i]['idCategory'];
/*
* Get all child categories of the parent (categories with
* an idCategoryParent equal to value of parent category's idCategory).
*/
var children = Categories.find({idCategoryParent:pId});
var cCount = children.count();
/*
* Assign the child categories array as a property of the parent category
* so that we can access it easily in the template #each expression
*/
parents.db_objects[i]['children'] = children;
}
return parents;
}
// This is our template
<template name="content">
<h1>Categories</h1>
{{#each categories_parents}}
<b>{{defaultLabel}}</b><br />
<ul>
{{#each children}}
<li>{{defaultLabel}}</li>
{{/each}}
</ul>
{{/each}}
</template>
我在故障排除时尝试的其他模板配置:
{{#each children}}
<li>A Child Exists Here</li> // Even this never rendered... no children?
{{/each}}
如果您能提供任何有关发生这种情况的线索,将不胜感激。
{{#each}}
助手传递一个参数,因此第一个示例应该可以工作(在 Meteor 1.4 上测试过)。 - Sphinxxx