概要:
親カテゴリ内にネストされた子カテゴリは、Meteorテンプレートにレンダリングされません。
詳細:
「カテゴリ」のデータモデルを次のように検討します。
// 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}}
なぜこれが起こっているのかについての手がかりをいただければ幸いです。