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.
My initial attempt at using Session seems to have missed the mark or something is not clicking.
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.
<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.
First of all why dont instead of notRated
and rated
, you use true
/ false
?
Something like Session.setDefault('rated', 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 isRated }}
{{> Rating}}
{{/if}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.