【问题标题】:Use a template event to trigger a response in another template使用模板事件触发另一个模板中的响应
【发布时间】:2016-03-01 15:22:43
【问题描述】:

当模板中发生事件时,我想在另一个模板中做一些事情。

这是我的代码:

客户端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({ “点击”:功能(e){ // {{body}} 中没有发生任何事情 Template.body.content = 函数(){ // 我想做点什么...比如查询数据库 // 也许,只是一个例子: return peple.find({"name":e.currenTarget.value}); 返回“你好”; }; } });

知道我做错了什么吗?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    使用把手将数据传递给模板,以便模板系统知道何时重绘 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>
    

    【讨论】:

    • 非常完美。非常感谢你。但是没有会话还有其他方法吗?我担心会话中会有很多数据...
    • Session 并不是真正的“cookie”,或者说它只是一个哈希图,它也应该可以处理大量数据。如果您有大量数据,请使用另一个模板并使用更多车把助手来填充它,而不是 {{content}} 使用 {{date}} 例如日期和每个单独的变量。如果您想将其用于内容,请使用 Meteor Router 等路由器
    猜你喜欢
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2016-05-10
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多