[英]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.