【问题标题】:Update parent template's dynamic template include from child template从子模板更新父模板的动态模板包含
【发布时间】:2014-12-19 20:55:39
【问题描述】:

我是 Meteor 的新手,需要一些帮助。

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

<div id="parentTemplate">

   {{>yield}}

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

      {{> UI.dynamic template=rightSideMenu}}

   </div>

</div>

因此,由于我使用的是 Iron-router,当一个新的子模板从 Iron-router 注入“yield”时,右侧菜单需要根据它现在所在的“页面”进行更改。 我试图弄清楚如何从子模板更新父动态模板幻灯片菜单。我可以用 Angular 很容易地做到这一点……但我似乎无法用 Meteor 解决这个问题。

任何帮助将不胜感激!

【问题讨论】:

  • 请注意,从 0.9.4 开始,UI.dynamic 已被替换为 {{&gt; Template.dynamic }}
  • 使用 Iron Router regions 怎么样?
  • 谢谢萨莎,我以前从未听说过他们。我会调查的。感谢您的建议!

标签: javascript meteor meteor-blaze


【解决方案1】:

好的,我确实让这个工作了。我在此博客上找到了解决方案:

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]);

    };

});

【讨论】:

    【解决方案2】:

    使用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') 相应地获取当前“页面”。

    【讨论】:

    • 嗨,丹,感谢您的回复。我尝试像您建议的那样使用 Session 从子模板设置它,但不幸的是它不起作用。我什至尝试在 Tracker.autorun 中包装对 Session.get 的调用,但它仍然不会更新。 :-/
    猜你喜欢
    • 2014-04-12
    • 2012-11-03
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2013-10-18
    • 2012-04-17
    相关资源
    最近更新 更多