【问题标题】:Meteor: hide/show template on Mongo DB queryMeteor:在 Mongodb 查询中隐藏/显示模板
【发布时间】: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


    【解决方案1】:

    首先为什么不使用notRatedrated 而不是true / false

    类似Session.setDefault('rated', false');

    然后做这样的事情。

    Tracker.autorun(function(){
      Session.set('rated', userHasRated()); //since this returns true or false
    });
    

    那你就可以做一个这样的助手了。

    Template.ShowTake.helpers({
     isRated: function(){
      return Session.get(); //or use a RegisterHelper 
     }
    });
    

    那么你的 if 将如下所示。

    {{#if isRated }}
        {{> Rating}}
    {{/if}}
    

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 2012-08-01
      相关资源
      最近更新 更多