【问题标题】:How to do this templating in emberjs?如何在 emberjs 中做这个模板?
【发布时间】:2014-10-16 14:37:55
【问题描述】:

我有一个应用程序模板,它在 section 标签中包含一个 outlet,还包括一些其他标签,如页眉、页脚等。当我根到 posts 路由和它时,就会使用 outlet使用PostsController。然后该页面将使用帖子模板填写。但是,当我导航到主路由 / 时,#main 部分为空白。

<header>My header</header>

<section id='main'>
  {{ outlet }}
</section>

<footer>My footer</footer>

当我在回家路线上时,我想使用那个插座来显示一些东西,但是当我导航到帖子时,让那个插座充满其他东西(比如帖子模板)。我尝试过添加:

{{#if isRoot}}
  lorem ipsum
{{/if}}

然后将其添加到applicationController:

App.ApplicationController = Ember.Controller.extend({
  isRoot: true
})

但即使我导航到posts 仍然是正确的(据我了解,因为其他控制器嵌套在应用程序控制器内)。解决这个问题的惯用方法是什么?

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    为这个确切的问题创建了索引路由。当你到达一个端点而不是更深时使用它,让你在那个端点处显示一些东西。

    App.IndexRoute = Em.Route.extend({
      // you can return a different model, or just use the application's model
    });
    
    
    <script type="text/x-handlebars" data-template-name="index">
      I'm only at the root of my app!
    </script>  
    

    【讨论】:

    • 我不想使用脚本标签,我只是使用车把花括号但是如果我添加App.IndexRoute = Em.Route.extend({ isRoot: true }); 它仍然不起作用。
    • 所以你想要非惯用的方式来做你的方式,而不是灰烬的方式?您可以查看 currentPath 属性,它仅在应用程序控制器上定义。您也可以使用计算相等来简单地设置它
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 2021-02-23
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多