Controllers & Templates

What is a Controller?

A Controller is routable object which receives a single property from the Route – model – which is the return value of the Route's model() method.

The model is passed from the Route to the Controller by default using the setupController() function. The Controller is then often used to decorate the model with display properties such as retrieving the full name from a name model.

A Controller is usually paired with an individual Route of the same name.

When should we create a Controller?

  • We want to pass down actions or variables to share with a Route’s child components

  • We have a computed property that depends on the results of the model hook

  • We need to support query parameters

Templates

Template and controller are essentially the same thing – two sides of the same coin. All the controller properties are the template’s.

Each template will be rendered into the {{outlet}} of its parent route's template. For example, the posts.new route will render its template into the posts.hbs's {{outlet}}, and the posts route will render its template into the application.hbs's {{outlet}}.

Last updated