简体   繁体   English

流星:Mongo DB查询上的隐藏/显示模板

[英]Meteor: hide/show template on Mongo DB query

I am looking for a way (or ways) to show or hide Meteor Handlebars html template based on the status of Mongo DB variable. 我正在寻找一种或多种基于Mongo DB变量状态显示或隐藏Meteor Handlebars html模板的方法。 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. 换句话说,如果用户提交了特定双关语的评分,则该评分将存储在Mongo集合中,并且“评分”模板应自行隐藏。 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. 我最初使用Session的尝试似乎错过了标记或没有点击。

Javascript Java脚本

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'. 经过测试之后,我确定无论会话变量“ rated”的状态如何,“ Rating”模板都可以显示。 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 ? 首先为什么不代替notRatedrated ,您可以使用true / false

Something like Session.setDefault('rated', false'); 类似于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}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM