【问题标题】:Meteor Handlebars: #if this equals - load different templates with helperfunctionMeteor Handlebars: #if this equals - 使用 helperfunction 加载不同的模板
【发布时间】:2013-07-03 06:25:40
【问题描述】:

我对 Meteor 还很陌生,只是尝试使用 Transitioner 构建我的第一个流星应用程序。

所以我要做的是在传递给另一个模板的字符串等于某个值时加载不同的模板。 HTML 代码:

<template name="onePage">
  {{#if itIs "teams"}}
    {{> teams}}
  {{/if}}
  {{#if itIs "players"}}
    {{> players}}
  {{/if}}
</template>
<template name="teams">
  <h2>These are the TEAMS</h2>
</template>
<template name="players">
  <h2>These are the PLAYERS</h2>
</template>

JS代码:

Template.onePage.itIs = function(passed) {
  return this === passed;
};

由于某种原因这不起作用,我只是不明白为什么。 唯一的一点是传递给 onePage 模板的“this”是一些字符串(来自 URL)。如果这个字符串是“teams”我想加载teams模板,如果是“players”我想加载players模板。

就这么简单! :-)

很遗憾,我无法解决这个简单的问题。

希望你们能理解我的问题并提供帮助!

最好的问候 帕特里克

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    Template.onePage.itIs 回调中的this 是什么?

    如果您将其更改为 Session(例如,您从 URL 设置,可能使用 Meteor Router),那么代码可能会正常工作!

    因此,例如,/teams URL 会将名为 showThisPage 的 session 设置为“teams”,/players URL 会将 showThisPage 会话设置为“players”,Template.onePage.itIs 将检查传递的值反对本次会议。

    类似这样的东西(我只是直接将其输入堆栈溢出而不对其进行测试,但它应该可以工作):

    Meteor.Router.add({ 
       '/teams': function() { Session.set('showThisPage', 'teams'); }, 
       '/players': function() { Session.set('showThisPage', 'players'); } 
    });
    
    Template.onePage.itIs = function(passed) {
      return Session.get('showThisPage') === passed;
    };
    

    没有像我说的那样经过测试,但它应该为您指明正确的方向。

    (您可以使用 Meteor.Router.page() 而不是使用会话,但您可以在 Meteor Router page 上阅读所有相关信息。)

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多