从子模板更新父模板的动态模板包含

时间:2014-10-24 03:01:42

标签: javascript meteor meteor-blaze

我对Meteor很新,需要一些帮助。

我有一个包含侧幻灯片菜单的外部父模板。该侧幻灯片菜单正在使用新的Blaze动态模板包含。

<div id="parentTemplate">

   {{>yield}}

   <div class="right-slide-menu">

      {{> UI.dynamic template=rightSideMenu}}

   </div>

</div>

因为我使用Iron-router,当一个新的子模板注入&#34; yield&#34;从铁路由器右侧菜单需要改变,取决于&#34;页面&#34;它现在开始了。 我试图找出如何从子模板更新父动态模板幻灯片菜单。我可以使用Angular轻松地做到这一点......但是我似乎无法使用Meteor来解决这个问题。

任何帮助都会非常感激!

2 个答案:

答案 0 :(得分:4)

好的,我实际上已经开始工作了。我在这个博客上找到了解决方案:

http://empire5.com/development/meteor-rendering-a-handlebars-template-with-dynamically-loaded-data/

不推荐使用UI.render和UI.insert(尽管它现在仍然有效)所以我将他的指示调整为新的方式:

after upgrading to meteor 0.9.1 i keep getting "Warning: Blaze.insert has been deprecated."

所以最终的代码如下:

<div id="parentTemplate">

   {{>yield}}

   <div class="right-slide-menu">

      <div class="sideMenu">

        <!-- side menu template gets injected here -->

      </div>

   </div>

</div>

在我的孩子模板经理里面,我有这个:

Template.dashboard.events({

    'click #showSideMenuBtn' : function(e){
        e.preventDefault();

        // pass in the name of the template you want to inject and 
        // also the parent container you want to inject it into
        Blaze.render(Template.addManagerForm, $('.sideMenu')[0]);

    };

});

答案 1 :(得分:1)

使用Session变量?

Template.parentTemplate.helpers({
  // assuming two templates...
  whichMenu: function () {
    return Session.get('whichMenu') ? 'menuTemplate1' : 'menuTemplate2'
    // or just Session.get('menuTemplate'), and you store the template name there
  }
});

然后

<template name="parentTemplate">
  {{> Template.dynamic template=whichMenu}}
</template>

您可以使用Router.current()Session.set('menuTemplate')来获取当前的“页面”。