简体   繁体   English

EmberJS-将单独的路线作为模式呈现到特定的出口

[英]EmberJS - Rendering separate route as a modal into a specific outlet

In my EmberJS applications I have two separates routes as follows, 在我的EmberJS应用程序中,我有两条单独的路线,如下所示:

Route A - "main/books/add" Route B - "main/authors/add" 路线A-“主要/书籍/添加”路线B-“主要/作者/添加”

I have an "Add Authors" button In Route A template and when a user presses that button I want to load and render Route B in a modal to add new authors. 我在Route A模板中有一个“添加作者”按钮,当用户按下该按钮时,我想以模态加载和渲染Route B以添加新作者。

I know its possible to achieve somewhat smiler to this by using the route's render method to render the Route B template and respective controller. 我知道可以通过使用路由的render方法来呈现Route B模板和相应的控制器来对此有所微笑。 But in that case, the "model" hook of Route B in "main/authors/add.js" file does not get invoked. 但是在这种情况下,不会调用“ main / authors / add.js”文件中路径B的“模型”钩子。

It would be really nice if someone can suggest me a method to render a separate route into a modal. 如果有人可以向我建议一种将单独的路线渲染为模态的方法,那就太好了。

EDIT - Although this is entirely valid (the premise of rendering into using named outlets, views are now deprecated in Ember 1.1. The same can be achieved by using a Component 编辑 - 尽管这是完全有效的(使用命名的插座进行渲染的前提,但现在在Ember 1.1中已弃用视图。使用Component可以实现相同的目的

Yup, you can do this: 是的,您可以这样做:

What you'd want to do is create a modal in a template and assign a named outlet into it (or create a view that is a modal with an outlet): 您想要做的是在模板中创建一个模态,并在其中分配一个命名插座(或创建一个带有插座的模态视图):

in modal.hbs : modal.hbs

<div class='modal'>
    {{outlet "modalContent"}}
</div>

Then I would override your base button like so: 然后,我将像这样覆盖您的基本按钮:

App.BasicButton = Em.View.extend({
    context: null,
    template: Em.Handlebars.compile('<button>Click Me!</button>');
    click: function(evt) {
        this.get('controller').send('reroute', this.get('context'));
    }
});

And in your template set up your button to trigger your modal: 并在模板中设置您的按钮以触发模态:

in trigger.hbs trigger.hbs

<!-- content and buttons for doing stuff -->
{{View App.BasicButton context='modalContent'}}

Finally, you want to create a method in your route which handles rendering specific content into your outlet: 最后,您要在路由中创建一个方法,以将特定内容呈现到插座中:

App.TriggerRoute = Em.Route.extend({
    actions: {
        reroute: function(route) {
            this.render(route, {into: 'modal', outlet: route});
        }
    }
});

So in essence, you're rendering the template (called "modalContent") into a specific outlet (called "modalContent"), housed within the template/view (called "modal") 因此,从本质上讲,您是将模板(称为“ modalContent”)呈现到位于模板/视图(称为“ modal”)中的特定出口(称为“ modalContent”)中。

You would also want to write some logic to trigger the modal to open on element insertion. 您还需要编写一些逻辑来触发在插入元素时打开模式。 To do that, I would use the didInsertElement action in the modal view: 为此,我将在modal视图中使用didInsertElement操作:

App.ModalView = Em.View.extend({
    didInsertElement: function() {
        this.$.css("display", "block");
        //whatever other properties you need to set to get the modal to pop up
    }
});

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

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