简体   繁体   English

Ember.js渲染出口作为模态窗口

[英]Ember.js Rendering outlet as a modal window

I'm trying to render a modal. 我正在尝试渲染一个模态。 For that I've created a custom outlet using {{outlet modalOutlet}} My application template has two outlets, the default outlet and the modalOutlet. 为此我使用{{outlet modalOutlet}}创建了一个自定义插座我的应用程序模板有两个插座,默认插座和modalOutlet。 However when the modal template is rendered into {{outlet modalOutlet}} , my default {{outlet}} becomes empty. 但是,当模态模板呈现为{{outlet modalOutlet}} ,我的默认{{outlet}}变为空。

How do I change it, so that the default {{outlet}} doesn't change, so I can actually render {{outlet modalOutlet}} as modal window, or as a sidebar as a part of a layout 如何更改它,以便默认{{outlet}}不会更改,因此我实际上可以将{{outlet modalOutlet}}渲染为模态窗口,或作为边栏作为布局的一部分

I'm not sure if this is due to my code, or something about the renderTemplate() method that I'm missing. 我不确定这是由于我的代码,还是我缺少的renderTemplate()方法。 The jsFiddle with my code is here . 我的代码jsFiddle就在这里

// Router
App.Router.map(function(){
    this.route('contributors');
    this.route('contributor', {path: '/contributors/:contributor_id'});
});


App.ContributorsRoute = Ember.Route.extend({
    model: function() {
        return App.Contributor.all();
    },
});

App.ContributorRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render('contributor', {
            outlet: 'modalOutlet'
        });
    }
});

<script type="text/x-handlebars" data-template-name="application">  
    <nav>
        {{#linkTo "index"}}Home{{/linkTo}}
        {{#linkTo "contributors"}}Contributors{{/linkTo}}
    </nav>
    <div style='padding:5px;margin:5px;border:1px dotted red;'>
        Default Outlet
        {{outlet}}
    </div>
    <div style='padding:5px;margin:5px;border:1px dotted blue;'>    
        modalOutlet
        {{outlet modalOutlet}}
    </div>
</script>

You must render the contributors template as well, since the default outlet gets cleared when you transition to a sibling route. 您还必须渲染contributors模板,因为当您转换为兄弟路径时,默认插座会被清除。

App.ContributorRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render('contributors');
        this.render('contributor', {
            outlet: 'modalOutlet'
        });
    }
});

You can avoid this, however, if you nest your routes like this: 但是,如果您像这样嵌套路线,则可以避免这种情况:

App.Router.map(function(){
    this.resource('contributors', function() {
        this.route('show', {path: ':contributor_id'});
    });
});

...and adjust the rest of your app to match the new structure. ...并调整应用程序的其余部分以匹配新结构。 In this case, you need to specify the place the modalOutlet lies with the into option (in this case: 'application' ) 在这种情况下,您需要使用into选项指定modalOutlet所在的位置(在本例中为'application'

The issue is your routing structure is not nested, and once you nest your routes you will need to specify the route which contains the modal outlet. 问题是您的路由结构没有嵌套,一旦您嵌套路由,您将需要指定包含模态出口的路由。

What is happening is you render 发生的事情是你渲染

Application -> Contributors 申请 - >贡献者

to show your list, but when you click a link you are now rendering 显示您的列表,但是当您单击现在正在渲染的链接时

Application -> Contributor 应用程序 - >贡献者

and the Contributor template is removed. 并删除了Contributor模板。

If you nest your routes, like this: 如果您嵌套路线,如下所示:

Application -> Contributors -> Contributor 申请 - >贡献者 - >贡献者

Then you will still have the Contributors template showing the list. 然后,您仍然会有显示列表的Contributors模板。

updated JSFiddle 更新了JSFiddle

//Router
App.Router.map(function(){
    this.resource('contributors', function() {
        this.resource('contributor', {path: '/:contributor_id'});
    });   
});

//Route
App.ContributorRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render('contributor', {
            into: 'application',
            outlet: 'modalOutlet'
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM