我只想显示点击的日期
的注释
。
我的应用程序的结构如下:
homePage.html
<template name="homePage">
<h2>Welcome to home page!</h2>
{{#each posts}}
{{> postOnDay}}
{{/each}}
</template>
postOnDay.html
<template name="postOnDay">
<a href="#" class="aDay">{{day}}</a>
<div style="display:{{showPost}}">
{{note}}
</div>
</template>
homePage.js
Template.homePage.helpers({
posts: function() {
//alert(Posts.find().count());
return Posts.find();
}
});
postOnDay.js
Template.postOnDay.events({
'click .aDay': function(e) {
e.preventDefault();
var link = e.currentTarget;
showPost: 'inline' //change value on click
}
});
Template.postOnDay.helpers({
showPost: 'none' //default value
})
最佳答案
如果您想更改助手和事件处理程序中的值,您应该使用 session :
而不是
showPost: 'inline' //change value on click
}
});
Template.postOnDay.helpers({
showPost: 'none' //default value
})
我会这样做:
Session.set('showPost', 'inline'); //change value on click
}
});
Template.postOnDay.helpers({
showPost: function(){ return Session.get('showPost')}
})
对于默认值,请使用:
Session.setDefault('showPost', 'inline')
根据您的需要,这可以在模板或应用程序初始化中。
要考虑的另一件事是,如果您想要每个帖子都有一个变量,您可能需要使用类似以下内容的内容:
Session.get('showPost' + this.id)
请务必在控制台中输出 this
的值,以便确保每个帖子的 this.id 都是唯一的。
关于javascript - Meteor:通过点击事件更改变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25872810/