繁体   English   中英

循环中的把手Registerhelper在Meteor中不起作用

[英]Handlebar Registerhelper in a loop not working in Meteor

我正在尝试在数组中制作一组动态的车把帮手,但是它不起作用。 这是我的代码。 客户JS

Meteor.startup(function(){
  Meteor.call('getTabs', function(e, r){//This works and returns the array it should
  //At the moment, r returns ["Home", "Dorem"]

  Session.set('tabs', r); //Again, this part works

  for(i in r){
    var bn = r[i] + 'bool'; //The name of the session.get for each thing, e.g. 'Homebool'
    Session.set(bn, false);//Makes them all false
    Handlebars.registerHelper(bn + 'e',function(){//Different name for clarification purposes, e.g. 'Homeboole'
      return Session.get(bn); //Should return
    });

  }
  switchTabs('Home');

  Session.set('Homebool', true);//Makes Homebool true so it'll display automatically


});

  function switchTabs(templateName){
    var t = Session.get('tabs');
    for(i in t)
    {
      console.log(t[i] + ":" + templateName);
      console.log(t[i] + ":" + (t[i] == templateName) + " switch")
      if(t[i] == templateName)
        Session.set(t[i] + 'bool', true);
      else
        Session.set(t[i] + 'bool', false);
    }
  }


  Template.navbar.events({
      'click .tabSwitch' : function(e, t){
        console.log(e.currentTarget.id + "Current");
        switchTabs(e.currentTarget.id);
      }
  });
  Template.navbar.show = function(){//This makes a navigation bar with

    var ret = "<div class='pure-menu pure-menu-open pure-menu-horizontal'><a href='#' class='pure-menu-heading'>Hello World</a><ul>";
    var t = Session.get('tabs');
    for(i in t){
      ret+="<li id='aa'><a href='#' class='tabSwitch' id='" + t[i] + "'>" + t[i] + "</a></li>";
    }
    ret+="</ul></div>";
    console.log(ret);
    return ret;
  }

的HTML

<body>
{{> navbar}}
{{#if Homeboole}}
  {{> Home}}
{{/if}}

{{#if Doremboole}}
  {{> Dorem}}
{{/if}}

</body>

<template name="Home">
<p>Home</p>
</template>

<template name="Dorem">
<p>Dorem</p>
</template>

<template name="navbar">
{{{show}}}
</template>

当您单击“主页”或“ Dorem”标签时,内容将发生切换。 但是,仅当单击“ Dorem”元素时才会切换,并且当它切换时,它将显示“ Home”和“ Dorem”模板中的内容。 有人有解决方案吗? 谢谢。

我想到了。 我不得不采取一些不同的方法。 我没有制作一堆新的Handlebar处理程序,而是制作了一个带有arg的处理程序。

      Handlebars.registerHelper('test', function(a){
       return Session.get(a + 'bool');
      });

然后,在HTML中,我将像这样使用它。

{{#if test "Home"}}
  {{> Home}}
{{/if}}

{{#if test "Dorem"}}
  {{> Dorem}}
{{/if}}

暂无
暂无

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

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