如何从两个模板处理程序中监听一个事件?
How can I listen to one event from two template handlers?
例如,在下面的例子中,只调用了 foo
事件处理程序。 (这很奇怪——我本以为会调用 bar
事件处理程序,因为 <form>
元素在 bar
模板中。)
<template name="foo">
{{#bar klass="down"}}
<button type="submit">test</button>
{{/bar}}
</template>
<template name="bar">
<form>
{{> Template.contentBlock}}
</form>
</template>
Template.foo.events
'submit form' ->
alert 'foo'
Template.bar.events
'submit form' ->
alert 'bar'
看看 this 包,用这个你只声明 1 个事件处理程序,ho 将在你想要的任何模板上可用。
文档中的示例。
//here we declare 1 only event
Template.foo.events({
'click button': function (event, template) {
console.log("foo button clicked");
}
});
//here we inherit
Template.foo2.inheritsEventsFrom("foo"); //where foo2 is another Template, and now have access to the event from foo template
祝你好运
如何做到这一点的答案是问题中的代码,它实际上可以按预期工作。 bar
处理程序被调用,然后是 foo
处理程序。
例如,在下面的例子中,只调用了 foo
事件处理程序。 (这很奇怪——我本以为会调用 bar
事件处理程序,因为 <form>
元素在 bar
模板中。)
<template name="foo">
{{#bar klass="down"}}
<button type="submit">test</button>
{{/bar}}
</template>
<template name="bar">
<form>
{{> Template.contentBlock}}
</form>
</template>
Template.foo.events
'submit form' ->
alert 'foo'
Template.bar.events
'submit form' ->
alert 'bar'
看看 this 包,用这个你只声明 1 个事件处理程序,ho 将在你想要的任何模板上可用。
文档中的示例。
//here we declare 1 only event
Template.foo.events({
'click button': function (event, template) {
console.log("foo button clicked");
}
});
//here we inherit
Template.foo2.inheritsEventsFrom("foo"); //where foo2 is another Template, and now have access to the event from foo template
祝你好运
如何做到这一点的答案是问题中的代码,它实际上可以按预期工作。 bar
处理程序被调用,然后是 foo
处理程序。