流星递归模板不起作用 [英] Meteor recursive template doesn't work
问题描述
我试图拥有一个像这样的递归模板:
I tried to have a recursive template like this:
<template name="generate">
{{#if elements}}
{{#each elements}}
<div>{{#if elements}}{{> generate}}{{/if}}</div>
{{/each}}
{{else}}
{{> generate elements=getElements}}
{{/if}}
</template>
与助手一起:
Template.generate.helpers({
getElements: function() {
return Elements.find()
}
})
和元素"数据对象:
[{
_id : "fgQ4GHrrZGFFGWPZQ",
elements" : [{
_id : "hY8iAYJC4KBwGKN84",
elements : []
}]
},{
_id : "rtMNfaQYwqzNTYqoD",
elements : [{
_id : "p2wJeGdtiGMYBQtpW",
elements : []
}]
}]
我遇到一个问题,键盘事件变得无响应,其他功能停止工作.模板不是设计用来处理这种递归吗?如果是这样,我将尝试其他方法,但是我认为这可以工作.其他人看到这个或有什么建议吗?谢谢!
I'm running into an issue where the keyboard events become unresponsive and other functionality ceases to work. Were templates not designed to handle this kind of recursion? If so, I'll try a different approach but I figured this would work though. Anyone else seeing this or have any suggestions? Thanks!
这将起作用.我的问题是在渲染的"回调上设置了一个键盘事件处理程序,该事件被多次调用(每次渲染模板时),这导致了我提到的问题.我将删除此问题,但stackoverflow.谢谢大家的预算!
This will work. My problem was setting up a keyboard event handler on the "rendered" callback which was being called more than once (each time the template was rendered) which caused the issue I mentioned. I would delete this question but stackoverflow. Thanks everybuddy!
推荐答案
您在这里有一些错误.
首先.您有一个名为getElements
的助手,并且您将其称为elements
First. you have a helper named getElements
and you are calling it like elements
第二,您正在将模板调用为同一模板{{> generate}}
Second you are calling the template into the same template {{> generate}}
<template name="generate">
{{#if elements}}
{{#each getElements}}
<div>{{#if elements}}{{> anotherTemplateName}}{{/if}}</div>
{{/each}}
{{else}}
{{> generate elements=getElements}}
{{/if}}
</template>
elements
助手的外观也如何? {{#if elements}}
Also how the elements
helper looks? {{#if elements}}
建议在此处使用嵌套模板.
A suggestion will be using nested templates here.
<template name="generate">
{{#if elements}}
{{> generateExample}}
{{else}}
{{> generate elements=getElements}}
{{/if}}
</template>
<!-- Generate Example Template -->
<template name="generateExample">
{{#each getElements}}
<div>{{#if elements}}{{> anotherTemplateName}}{{/if}}</div>
{{/each}}
</template>
看看这个了解空格键以获得更好的学习体验
Take a look into this Understanding Spacebars for a better learning experience
这篇关于流星递归模板不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!