I am looking for a way (or ways) to show or hide Meteor Handlebars html template based on the status of Mongo DB variable. In other words, if a user submits a rating for a particular pun, then that rating is stored in a Mongo collection and the 'rating' template should hide itself. Therefore the "hide" could be triggered by a number of conditions, such as right after a user submits a rating for the first time, or at onload if the user happens to load a previously-rated pun.
我正在寻找一种方法(或方法)根据Mongo DB变量的状态显示或隐藏Meteor Handlebars html模板。换句话说,如果用户提交特定双关语的评级,则该评级存储在Mongo集合中,并且“评级”模板应隐藏自身。因此,“隐藏”可以由许多条件触发,例如在用户第一次提交评级之后立即触发,或者如果用户恰好加载先前评级的双关语则触发。
My initial attempt at using Session seems to have missed the mark or something is not clicking.
我最初使用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: the three lines beginning with {{/if isRated 'notRated'}} is where the trouble lies.
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>
After testing, I've determined that the 'Rating' template shows regardless of the state of Session variable 'rated'. Any thoughts are welcome. Also, if I have way over-complicated the issue, I am certainly open to more elegant answers.
经过测试,我已经确定“评级”模板显示的是会话变量'rating'的状态。欢迎任何想法。此外,如果我过分复杂的问题,我肯定愿意接受更优雅的答案。
1 个解决方案
#1
0
First of all why dont instead of notRated
and rated
, you use true
/ false
?
首先,为什么不使用notRated和rating,你使用true / false?
Something like Session.setDefault('rated', false');
类似于Session.setDefault('rating',false');
Then make something like this.
然后做这样的事情。
Tracker.autorun(function(){
Session.set('rated', userHasRated()); //since this returns true or false
});
Then you can do a helper like this.
然后你可以这样做一个帮手。
Template.ShowTake.helpers({
isRated: function(){
return Session.get(); //or use a RegisterHelper
}
});
Then your if will look like this.
然后你的if会是这样的。
{{#if isRated }}
{{> Rating}}
{{/if}}
#1
0
First of all why dont instead of notRated
and rated
, you use true
/ false
?
首先,为什么不使用notRated和rating,你使用true / false?
Something like Session.setDefault('rated', false');
类似于Session.setDefault('rating',false');
Then make something like this.
然后做这样的事情。
Tracker.autorun(function(){
Session.set('rated', userHasRated()); //since this returns true or false
});
Then you can do a helper like this.
然后你可以这样做一个帮手。
Template.ShowTake.helpers({
isRated: function(){
return Session.get(); //or use a RegisterHelper
}
});
Then your if will look like this.
然后你的if会是这样的。
{{#if isRated }}
{{> Rating}}
{{/if}}