Javascript 在meteor中设置简单的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10112930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting simple events in meteor
提问by Sofia
I'm trying out Leaderboard's example in Meteor but i'm doing something wrong in setting the click event. In this example, I have three buttons, one to change the sort by column, another to add 5 bonus points to everyone.
我正在 Meteor 中尝试排行榜的示例,但在设置点击事件时我做错了。在此示例中,我有三个按钮,一个用于更改按列排序,另一个用于向每个人添加 5 点奖励积分。
Here's the html:
这是html:
<div id="outer">
{{> sorter}}
{{> leaderboard}}
</div>
<template name="sorter">
<span>Sorted by {{sortedBy}}</span>
{{#if sortByName}}
<input type="button" id="sortScore" value="sort by score" />
{{else}}
<input type="button" id="sortName" value="sort by name" />
{{/if}}
<input type="button" class="incAll" value="5 bonus points to all" />
</template>
And here's the js:
这是js:
Template.sorter.events = {
'click #sortName': function(){
Session.set('orderby', 'name');
},
'click #sortScore': function(){
Session.set('orderby', 'score');
},
'click input.incAll': function(){
Players.find().forEach(function(player){
Players.update(player._id, {$inc: {score: 5}});
});
}
}
}
Calling Session.set('orderby', 'name'); in the console works and updates the html accordingly but clicking in the buttons doesn't. So what am I missing?
调用 Session.set('orderby', 'name'); 在控制台中工作并相应地更新 html 但单击按钮不会。那么我错过了什么?
Thanks
谢谢
回答by debergalis
Event maps with selectors won't match top-level elements in a template. This is something we will fix ASAP.
带有选择器的事件映射不会匹配模板中的顶级元素。这是我们将尽快修复的问题。
There's an easy workaround though. Wrap your sorter template in a <div>
.
不过有一个简单的解决方法。将您的分拣机模板包装在<div>
.