流星:如何访问嵌套模板中的父属性? [英] Meteor: how to access parent properties within nested templates?
问题描述
我开始使用Meteor,并修改了 todo示例以包含嵌套标记组.我有以下HTML,它输出每个标签组的每个名称,以及每个组中的标签列表:
I am getting started with Meteor, and adapting the todo example to include nested tag groups. I have the following HTML, which outputs each name of each tag group, plus the list of tags in that each group:
<template name="tag_filter">
{{#each tag_types }}
{{ tag_name }}
{{#each values }}
<div data-taggroup="{{ ../tag_name }}">
{{ name }} ({{ count }})
</div>
{{/each}}
{{/each}}
</template>
我的问题是:如何使事件处理程序适应单击标签以访问父组的tag_name
的值? (即来自外部每个循环的数据).
My question is this: how do I adapt the event handler for clicks on the tags to access the value of the parent group's tag_name
? (i.e. the data from the outer each loop).
当前我有下面的代码,但是this
对象仅使我可以访问name
和count
.
Currently I have the code below, but this
object only gives me access to name
and count
.
Template.tag_filter.events({
'mousedown .tag': function () {
console.log('tag mousedown', this);
// How do I get the value of tag_name?
}
});
如您所见,我已经使用把手父路径添加了一个data-taggroup
属性,该属性包含名称,但是我不确定如何从事件处理程序中访问它.
As you can see, I've used Handlebars parent paths to add a data-taggroup
attribute containing the name, but I'm not sure how to access that from within the event handler.
我认为这个问题是相关的,但我不理解OP的解决方案(部分是因为我没有使用Coffeescript).还有一个与此相关的封闭流星问题.
I think this question is related, but I don't understand the OP's solution (partly because I'm not using Coffeescript). There's also an closed Meteor issue which is related.
推荐答案
我不确定是否可以获取父模板数据,但是在事件处理程序中,您可以访问DOM元素: event.currentTarget 将获得clicked元素.然后,只需使用jQuery即可访问属性.如果需要, event.currentTarget.parentNode 将获取DOM中的父元素.
I'm not sure whether you can get parent template data, but in your event handler you can access DOM elements: event.currentTarget will get the clicked element. Then just use jQuery to access attributes. If needed, event.currentTarget.parentNode will get the parent element in the DOM.
例如:我不知道您将标签类放在哪里,但可以说它是div的类,其中定义了数据标签组.然后,您可以使用以下方式获取标签名称:
Ex: I don't know where you put the tag class, but let's say it's the class of your div where data-taggroup is defined. Then you could get the tag name using:
$(event.currentTarget).attr('data-taggroup')
这篇关于流星:如何访问嵌套模板中的父属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!