Emberjs Modals on a route

蓝咒 提交于 2019-12-06 09:20:29

Here is how we handle it:

In the route you use a render piece similar to what you described:

App.UserDeleteRoute = Ember.Route.extend({
  renderTemplate: function() {    
    this.render({
      into: 'application',
      outlet: 'modal'
    });    
  },

  actions: {
    closeModel: function() {
      this.transitionTo('users.index');
    }
  },
  deactivate: function() {
    this.render('empty', {
      into: 'application',
      outlet: 'modal'
    });
  }  
}

Clearing out outlet on the "deactivate" hook is an important step.

For the main template all you need is:

{{outlet}}
{{outlet "modal"}}

This does not override the default outlet.

One thing you may want to do for all your modals is implement a layout:

App.UserDeleteView = Ember.View.extend({
  layoutName: 'settings/modals/layout',
  templateName: 'settings/modals/user_delete'
});

Another gotcha is that there needs to be a parent route rendered into the main outlet. If whatever is in that outlet is not the parent of your modal, then the act of moving away from that route will remove it from the outlet.

working JS bin

You've got several options here:

  • make modal route child of route you want to persist - than either render modal nested in application template, like in example you mentioned, or put it inside its own template id="myModal"
  • add modal outlet inside persisted route's outlet and render it in renderTemplate method

    renderTemplate: function(controller, model) {
        this.render();   //render default template
    
        this.render('myModalTemplate', { //render modal to its own outlet
            outlet: 'modal', 
            into: 'myModal', //parent view name. Probably same as route name
            controller : controller
        });
    }
    

Besides, you can render template with modal in named outlet any moment(on action f.e.), by just calling render method with proper arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!