The following calls messages when the page loads but not when a Session value is changed. Here is the template code;
以下在页面加载时调用消息,但在更改会话值时调用消息。这是模板代码;
<head>
<title>Your welcome to chat</title>
</head>
<body>
{{> chat}}
</body>
<template name="chat">
<div class="ui list" style='height: 100px; overflow:scroll;' id='chatHistory'>
{{> currentChatList}}
</div>
<div class="ui large icon input">
<input type="text" class="field" id="message" placeholder="Type your message here" />
<i class="comment icon"></i>
</div>
</template>
<template name="currentChatList">
{{#each messages}}
{{/each}}
</template>
Here is the code;
这是代码;
if (Meteor.isClient) {
Template.chat.events ({
'keydown input#message' : function (event) {
if (event.which == 13) { // 13 is the enter key event
Session.set('time', new Date());
}
}
});
Template.currentChatList.helpers({
messages : function () {
debugger;
return [];
}
});
}
debugger statement is hit when the page loads, but not when the enter key is pressed on the textbox and Session.set('time'..) is executed. I thought a Session value change would cause the template to render.
页面加载时会调用调试器语句,但是当在文本框上按下回车键并且执行Session.set('time'..)时则不会。我认为会话值更改会导致模板呈现。
2 个解决方案
#1
You do not depend on the Session
variable you're assigning anywhere on your code. A reactive computation such as template helpers only reruns if one of its dependent reactive data sources is modified, which is clearly not the case in your example.
您不依赖于您在代码中的任何位置分配的Session变量。诸如模板助手之类的反应计算仅在其一个依赖的被动数据源被修改时才重新运行,这在您的示例中显然不是这种情况。
Try to depend on the Session
variable inside your helper code to make it re-execute whenever you press enter.
尝试依赖助手代码中的Session变量,以便在按Enter键时重新执行它。
Template.currentChatList.helpers({
messages: function () {
debugger;
var time = Session.get("time");
console.log("You pressed enter at",time);
return [];
}
});
#2
saimeunt's answer will solve you problem. Your helper at the moment doesn't access any reactive variable (Session or Collection), which means it's never executed again.
saimeunt的答案将解决您的问题。您的助手此刻不会访问任何反应变量(会话或集合),这意味着它永远不会再次执行。
Recently more and more people prefer to use reactive-var:
最近越来越多的人喜欢使用reactive-var:
- The idea is that you declare a reactive variable within your JS file and then USE that reactive variable in your helper. Every time you change the variable, all helpers using this variable are re-executed.
我们的想法是在JS文件中声明一个反应变量,然后在你的帮助器中使用该反应变量。每次更改变量时,都会重新执行使用此变量的所有帮助程序。
#1
You do not depend on the Session
variable you're assigning anywhere on your code. A reactive computation such as template helpers only reruns if one of its dependent reactive data sources is modified, which is clearly not the case in your example.
您不依赖于您在代码中的任何位置分配的Session变量。诸如模板助手之类的反应计算仅在其一个依赖的被动数据源被修改时才重新运行,这在您的示例中显然不是这种情况。
Try to depend on the Session
variable inside your helper code to make it re-execute whenever you press enter.
尝试依赖助手代码中的Session变量,以便在按Enter键时重新执行它。
Template.currentChatList.helpers({
messages: function () {
debugger;
var time = Session.get("time");
console.log("You pressed enter at",time);
return [];
}
});
#2
saimeunt's answer will solve you problem. Your helper at the moment doesn't access any reactive variable (Session or Collection), which means it's never executed again.
saimeunt的答案将解决您的问题。您的助手此刻不会访问任何反应变量(会话或集合),这意味着它永远不会再次执行。
Recently more and more people prefer to use reactive-var:
最近越来越多的人喜欢使用reactive-var:
- The idea is that you declare a reactive variable within your JS file and then USE that reactive variable in your helper. Every time you change the variable, all helpers using this variable are re-executed.
我们的想法是在JS文件中声明一个反应变量,然后在你的帮助器中使用该反应变量。每次更改变量时,都会重新执行使用此变量的所有帮助程序。