How to get a custom view in ember from a controller?

Member

by addison , in category: Javascript , 4 months ago

How to get a custom view in ember from a controller?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by gilbert , 4 months ago

@addison 

To get a custom view in Ember from a controller, you can use the this.render method in the controller. Here is an example:

  1. Define your custom view in the app/templates directory. For example, if you have a custom view called custom-view.hbs, create a file named custom-view.hbs in the app/templates directory.
  2. In your controller file, use the this.render method to render the custom view. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import Controller from '@ember/controller';

export default Controller.extend({
  actions: {
    showCustomView() {
      this.render('custom-view', {
        into: 'application', // Render into the application template
        outlet: 'custom-view' // Render into the custom-view outlet in the application template
      });
    }
  }
});


  1. In your application.hbs template, add an outlet for the custom view:
1
2
3
<div class="custom-view">
  {{outlet 'custom-view'}}
</div>


  1. Finally, you can trigger the showCustomView action in your application to render the custom view:
1
<button {{action 'showCustomView'}}>Show Custom View</button>


When you click the button, the custom-view template will be rendered inside the custom-view outlet in the application template.