尝试实现嵌套的模板结构-
<template name="parent">
<ul>
{{#if parenthelper}}
{{parenthelper}}
<li>
{{> child}}
</li>
{{/if}}
</ul>
</template>
<template name="child">
{{#if children.length}}
<ul>
{{#each children}}
{{children}}
<li>
{{> child}}
</li>
{{/each}}
</ul>
{{/if}}
</template>
和js
Template.parent.helpers({
'parenthelper': function(){
console.log("description is " + this.description);
return this.description;
}
});
Template.child.helpers({
'children': function(){
var child_list = [];
var data = Template.parentData();
console.log("id " + data._id);
if (data != null) {
if (typeof data._id != 'undefined') {
ItemList.find({parent: data._id}, {description:1, _id:0}).forEach(function(u) { child_list.push(u.description) });
console.log("childlist " + child_list);
return child_list;
} else {
return null;
}
} else {
return null;
}
}
});
它是如何工作的-我单击一个项目链接,该链接会加载页面,其中包含项目详细信息(mongo)和该项目下所有子项的递归列表。
作为一个例子,我单击了一个有一个子项而没有其他级别的项。我看到的调试输出对我来说并不完全清楚-
description is undefined
description is parentdesc
description is parentdesc
id undefined
为什么未定义id(Template.parentdata._id)?
示例数据:
{ "_id" : "2ENP9grLxHmCLNYfm", "username" : "hWvHzBCjs3TuttLMJ", "description" : "parent item 1", "startdate" : ISODate("2018-08-08T00:00:00Z"), "status" : "IN PROGRESS", "parent" : null }
{ "_id" : "PbZZHDusi5CYbKYhC", "username" : "hWvHzBCjs3TuttLMJ", "description" : "sub item 1", "startdate" : ISODate("2018-08-08T00:00:00Z"), "status" : "IN PROGRESS", "parent" : "2ENP9grLxHmCLNYfm" }