【发布时间】:2015-12-30 00:40:02
【问题描述】:
我刚刚开始为 Meteor 使用 templates:tabs package。默认情况下,这会生成一个作为{{#basicTabs tabs=tabs}} 插入的自定义模板。
我制作了一个简单的应用程序来了解这种自定义模板的工作原理,特别是了解这与使用 {{> custom}} 有何不同
HTML:
<body>
{{> parent1}}
{{> parent2}}
</body>
<template name="parent1">
<h1>Parent 1</h1>
{{> child}}
</template>
<template name="parent2">
<h1>Parent 2</h1>
{{#child}}
<h2>Stuff inside "child" tag</h2>
{{/child}}
</template>
<template name="child">
<button type="button">Child button</button>
</template>
JS:
if (Meteor.isClient) {
Template.child.events({
'click button': function (event, template) {
console.log("child", event, template)
}
});
Template.parent1.events({
'click button': function (event, template) {
console.log("parent1", event, template)
}
});
Template.parent2.events({
'click button': function (event, template) {
console.log("parent2", event, template)
}
});
}
我没有看到任何 Stuff inside the "child" tag 出现。当我点击一个按钮时,我可以在浏览器控制台中看到父模板和子模板都可以对输入做出反应,但这两种情况都会发生。
我应该如何使用{{#custom}} ... {{/custom}} 语法?
编辑
以下是显示 {{#child}} 块如何工作的更新文件,与 events 和 helpers 相关:
<body>
{{> parent1}}
{{> parent2}}
</body>
<template name="parent1">
<h1>Parent 1</h1>
{{> child}}
</template>
<template name="parent2">
<h1>Parent 2</h1>
{{#child}}
<h2>Stuff inside "child" tag</h2>
{{else}}
<h3>Other stuff</h3>
{{/child}}
</template>
<template name="child">
{{extras}}
{{> Template.contentBlock}}
{{> Template.elseBlock}}
<button type="button">Child button</button>
</template>
JS:
if (Meteor.isClient) {
Template.child.events({
'click button': function (event, template) {
console.log("child", event, template)
}
});
Template.parent1.events({
'click button': function (event, template) {
console.log("parent1", event, template)
}
});
Template.parent2.events({
'click button': function (event, template) {
console.log("parent2", event, template)
}
});
Template.child.helpers({
extras: function () {
return "Child extras"
}
})
Template.parent1.helpers({
extras: function () {
return "Parent 1 extras"
}
})
Template.parent2.helpers({
extras: function () {
return "Parent 2 extras"
}
})
}
使用 Meteor 1.2.0.2 输出:
父母 1
儿童附加功能 [儿童按钮]
父母 2
儿童剧
“子”标签内的东西
其他东西
[子按钮]
【问题讨论】: