当模板中发生事件时,我想在另一个模板中执行某些操作。
这是我的代码:
客户端 HTML:
<body> Navigation: {{> navigation}} ======================================================= Body: {{> body}} </body> <template name="navigation"> <input type="button" value="MenuBtn" /> </template> <template name="body"> {{content}} </template>
JavaScript:
Template.navigation.events({ 'click' : function (e) { //Nothing happened in {{body}} Template.body.content = function(){ // i want to do something... like query database // maybe, just a example : return peple.find({"name":e.currenTarget.value}); return "hello"; }; } });
知道我做错了什么吗?
最佳答案
使用句柄将数据传递给模板,以便模板系统知道何时重绘 html
客户端JS
Template.body.content = function() {
return Session.get("content") || "Hi there, click the button"
};
Template.navigation.events({
'click' : function () {
alert("1");// it's normal
Session.set("content", "<h1>HELLO!!!! '+ people.findOne({"name":e.currenTarget.value}).name || " unregisterd person" + '</h1>");
alert("2");//it's normal
}
});
我还假设您要执行一些 HTMLey 操作,并且您不希望它被转义,因此请确保使用:
<template name="body">
{{{content}}}
</template>
关于javascript - 使用模板事件触发另一个模板中的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15217491/