【发布时间】:2016-05-03 21:27:34
【问题描述】:
我正在寻找一种基于 Mongo DB 变量状态来显示或隐藏 Meteor Handlebars html 模板的方法。换句话说,如果用户提交了对特定双关语的评分,那么该评分将存储在 Mongo 集合中,并且“评分”模板应该隐藏自己。因此,“隐藏”可以由多种条件触发,例如在用户第一次提交评分之后,或者如果用户碰巧加载了之前评分的双关语,则在加载时。
我最初使用 Session 的尝试似乎没有成功,或者没有点击。
Javascript
Session.setDefault('rated', 'notRated');
function ratingDisplay () {
var whetherRated = userHasRated();
// userHasRated() is a function that returns boolean 'true' is the user has rated or boolean 'false' if not
if (whetherRated === true) {
Session.set('rated', 'rated');
} else {
Session.set('rated', 'notRated');
}
}
Handlebars.registerHelper('isRated', function (input) {
return Session.get('rated');
});
UI.body.helpers({
isRated: function (rating) {
return Session.equals('rated', rating);
}
});
HTML:以 {{/if isRated 'notRated'}} 开头的三行是问题所在。
<template name="ShowTake">
<div class="container">
<h1 class="item">take a pun</h1>
{{#with randomPun}}
<p class="item">{{prompt}}</p>
<p class="item">{{answer}}</p>
{{/with}}
{{#if isRated 'notRated'}}
{{> Rating}}
{{/if}}
<button class="item btn" data-action="getPun">more pun</button>
<button class="item btn clickChangesPage" data-page="showMain">return</button>
</div>
</template>
经过测试,我确定无论会话变量“额定”的状态如何,“评分”模板都会显示。欢迎任何想法。此外,如果我的问题过于复杂,我当然愿意接受更优雅的答案。
【问题讨论】:
标签: javascript mongodb templates meteor