【问题标题】:How to render partial template when particular menu clicked in Meteor JS在 Meteor JS 中单击特定菜单时如何呈现部分模板
【发布时间】:2014-09-21 14:17:03
【问题描述】:

我是 Meteor JS 的新手。单击菜单时,如何将特定模板呈现到 Web 应用程序的特定部分。我正在使用 Iron-router 和布局模板。 以下布局工作正常。例如:当用户点击“主页”>“文章”菜单时 ,我喜欢将文章模板渲染到 mainContent 区域(地址栏看起来像 /myapp/Article)。其他菜单项也一样,当单击菜单项时,特定模板会显示在 mainContent 部分。我怎样才能路由这个?我不确定是否有可能,是否有任何其他方法或更好的解决方案来解决这个问题。 路由器.js

Router.map(function(){this.route('home', {
path: '/',
layoutTemplate: 'homePageLayout',
yieldTemplates: {
  'myHeader': {to: 'header'},
  'mySideMenu': {to: 'sideMenu'},
  'myMainContent': {to: 'mainContent'},
  'myFooter': {to: 'footer'}
}

}); });

layout.html

<template name="homePageLayout">
<div class="container">
    <div class="row">
        {{> yield  region='header'}}
    </div>
    <div class="row">
        <div class="col-lg-3">
            {{> yield region='sideMenu'}}
        </div>
        <div class="col-lg-6">
            {{> yield 'mainContent'}}
        </div>
    </div>
    <div class="row">
        <footer>
            {{> yield region='footer'}}
        </footer>
    </div>
</div>

sideMenu.html

<template name="mySideMenu">
<div class="content"></div>
    <div class="panel-group" id="accordion">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="glyphicon glyphicon-home"></span>
                        Home
                    </a>
                </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">
                    <table class="table">
                        <tbody>
                        <tr>
                            <td>
                                <a href="{{pathFor 'mission'}}"><span class="glyphicon glyphicon-pencil text-primary"></span>
                                    Article
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
                                    News
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
                                    Report
                                </a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                        Company
                    </a>
                </h4>
            </div>
            <div id="collapseThree" class="panel-collapse collapse">
                <div class="panel-body">
                    <table class="table">
                        <tbody>
                        <tr>
                            <td>
                                <a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
                                    Mission
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
                                    About us
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <a href="#"><span class="glyphicon glyphicon-pencil text-primary"></span>
                                    Contact
                                </a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

【问题讨论】:

    标签: javascript templates meteor yield iron-router


    【解决方案1】:

    你快到了:)

    <div class="col-lg-6">
        {{> yield 'mainContent'}}
    </div>
    

    改成

    <div class="col-lg-6">
        {{> yield }}
    </div>
    

    这是“主要”产量。铁路由会在这里渲染路由的模板。要识别 IR 应呈现的模板,您可以做 2 件事。

    什么都不做 -> 然后 Iron Router 会渲染一个与路由名称相同的模板。在您的情况下,它将呈现模板“家”。

    或为您的路由器指定模板:

    Router.map(function(){
        this.route('home', {
            path: '/',
            template: 'mySuperTemplateForThisRoute',
            layoutTemplate: 'homePageLayout',
            yieldTemplates: {
              'myHeader': {to: 'header'},
              'mySideMenu': {to: 'sideMenu'},
              'myMainContent': {to: 'mainContent'},
              'myFooter': {to: 'footer'}
            }
        });
    });
    

    【讨论】:

    • {{> yield }}
      我尝试使用它,但是当加载特定路线时,它只会显示该模板,它没有显示“myHeader”和“myFooter”模板。这就是为什么我的布局在我绘制时不起作用。
    • DerMambo 你是对的。我错过了我的 route.configure() 我修复了它,现在它工作正常。正是你所说的,谢谢。我真的很喜欢阅读你的博客,再次感谢你的辛勤工作。
    【解决方案2】:

    我正在处理这样的问题。 这是我的 router.js

            Router.map(function(){
      this.route('home', {
      path: '/',
      layoutTemplate: 'homePageLayout',
      action: function(){
        this.render('myHeader', {to: 'header'});
        this.render('mySideMenu', {to: 'sideMenu'});
        this.render('home', {to: 'mainContent'});
        this.render('myFooter', {to: 'footer'});
      }
    });
    
      this.route('showMission', {
        path: 'mission',
        layoutTemplate: 'homePageLayout',
        action: function(){
          this.render('myHeader', {to: 'header'});
          this.render('mySideMenu', {to: 'sideMenu'});
          this.render('mission', {to: 'mainContent'});
          this.render('myFooter', {to: 'footer'});
        }
      });
    
      this.route('showCompanyStructure', {
        path: 'companyStructure',
        layoutTemplate: 'homePageLayout',
        action: function(){
          this.render('myHeader', {to: 'header'});
          this.render('mySideMenu', {to: 'sideMenu'});
          this.render('companyStructure', {to: 'mainContent'});
          this.render('myFooter', {to: 'footer'});
        }
      });
    
      this.route('showDuties', {
        path: 'duties',
        layoutTemplate: 'homePageLayout',
        action: function(){
          this.render('myHeader', {to: 'header'});
          this.render('mySideMenu', {to: 'sideMenu'});
          this.render('duties', {to: 'mainContent'});
          this.render('myFooter', {to: 'footer'});
        }
      });
    });
    

    因此,当单击特定菜单链接时,它将将该模板呈现到“mainContent”字段。 它工作正常。但我认为会有更好的解决方案,我的解决方案是重复代码,它可能会导致 'myheader' 和 'myfooter' 模板加载不止一次。我不确定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      相关资源
      最近更新 更多