javascript - 使用模板事件触发另一个模板中的响应

标签 javascript meteor

当模板中发生事件时,我想在另一个模板中执行某些操作。

这是我的代码:

客户端 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/

相关文章:

javascript - 从具有 O(n) 的数组中获取最大的时间顺​​序下降、最小值和最大值

javascript - 我无法解析 getElementsByClass 函数

javascript - 如果 div 可见,则更改文本

javascript - meteor , '/users/insert' 已定义

node.js - Meteor 和 Socket IO 端口

javascript - 在 meteor 中实现更多负载的最佳实践

javascript - 为什么我会因为这个 Javascript 函数而失去利润

javascript - 当我点击一个链接时,只是试图显示和提醒

node.js - meteor 失败,无法从 npm-shrinkwrap 安装 npm-packages

javascript - 使用 Meteor.js 的集合平均函数