我尝试显示模型树结构化集合。但是evertime我运行它,浏览器崩溃就像有一个无限循环。但我没有看到错误:
集合
{ "_id" : "7PAzgmcTfrcodKNYd", "title" : "Article" }
{
"_id" : "ZnTn5pHT3GtHWqv4C",
"title" : "Category 1",
"parent" : "7PAzgmcTfrcodKNYd"
}
{
"_id" : "u3zyuHegsokAoDrQX",
"title" : "Element 1",
"parent" : "j9WmezrM7pQvSq93c"
}
{
"_id" : "Te7jhxv4aRoAFEkiv",
"title" : "Element 2",
"parent" : "ZnTn5pHT3GtHWqv4C"
}
{
"_id" : "j9WmezrM7pQvSq93c",
"title" : "Group",
"parent" : "ZnTn5pHT3GtHWqv4C"
}
模板
<template name="editor">
{{#each section}}
<div class="section">
<h2>{{title}}</h2>
{{>getElements}}
</div>
{{/each}}
</template>
<template name="getElements">
<ul>
{{#each children _id}}
<li>{{title}}
{{#if hasChildren _id}}
{{>getElements parent=_id}}
{{/if}}
</li>
{{/each}}
</ul>
</template>
助手
Template.getElements.helpers({
children: function(parentId){
return Articles.find({ parent: parentId });
},
hasChildren: function(id){
return Articles.find({ parent: id }).count() > 0;
}
});
Template.editor.helpers({
section: function(){
var articleId = Session.get('articleId');
return Articles.find({ parent: articleId });
}
});
我希望得到结果
<div class="section">
<h2>Category 1</h2>
<ul>
<li>Element1</li>
<li>Group
<ul>
<li>Element 2</li>
</ul>
</li>
</ul>
</div>
{{#if hasChildren _id}}
此_id应该是子元素的_id,而不是section元素的_id。这应该有用,对吧?