我的索引模板有两个导出,一个导出用于标题,另一个导出用于内容。内容中呈现的模板会根据正在查看的内容而变化。

在旧路由器中,这可以通过在拥有该模板的不同 Controller 上调用connectOutlet来完成。我不知道如何在新路由器中执行相同的操作。

有什么建议么?

最佳答案

通过研究,我得出以下结论:
假设您有一个定义如下的路由器:

App.Router.map(function(match) {
  match('/').to('index');
});

ApplicationTemplate:
<script type="text/x-handlebars">
{{outlet header}}
{{outlet}}
</script>

索引模板:
<script type="text/x-handlebars" data-template-name="index">
{{outlet dashboard}}
{{outlet spaces}}
</script>

现在,我们想要的是,当用户转到索引路由器时,页面应:
  • 将索引呈现到应用程序模板的主 socket 中,并将 header 插入应用程序模板的 header socket 中。
  • 渲染仪表板,将模板空间插入“索引模板”。

  • 为此,我们在indexRoute中编写以下代码
    App.IndexRoute = Em.Route.extend({
        renderTemplate: function(controller, model){
            //Render header into header outlet
            this.render('header',{
                outlet:'header'
            });
            //Render index into main outlet. If you comment out
            //this line, the code below fails
            this.render('index');
    
            //by using into, we can render into the index template
            //Note: The controller is optional.if not specified,
            //ember picks up controller for the given template.
            this.render('dashboard',{
                outlet:'dashboard',
                into:'index',
                controller:this.controllerFor('somethingElse', App.TestModel.find())
            });
            //controller is SpacesController
            this.render('spaces',{
                outlet:'spaces',
                into:'index'
            });
        }
    });
    

    关于ember.js - 如何在路由器v2中呈现路由的多个模板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14180958/

    10-14 15:43
    查看更多