Skip to content

Commit eb6d64c

Browse files
author
Jen Weber
authored
Update controller index, PR #46 closes #34
Update Controller Index
2 parents b106978 + d58c64f commit eb6d64c

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

guides/v3.1.0/controllers/index.md

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1-
## Controllers
1+
### What is a Controller?
22

3-
Controllers behave like a specialized type of Component that is rendered by
4-
the router when entering a Route.
3+
A [Controller](https://emberjs.com/api/ember/release/classes/Controller) is routable object which receives a single property from the Route – `model` – which is the return value of the Route's [`model()`](https://emberjs.com/api/ember/3.1/classes/Route/methods?anchor=model) method.
54

6-
The controller receives a single property from the Route – `model` – which is
7-
the return value of the Route's `model()` method.
5+
The model is passed from the Route to the Controller by default using the [`setupController()`](https://www.emberjs.com/api/ember/3.1/classes/Route/methods/setupController?anchor=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.
86

9-
To define a Controller, run:
7+
A Controller is usually paired with an individual Route of the same name.
108

9+
### Defining a Controller
10+
11+
We only need to generate a Controller file if we want to customize the properties or provide any actions to the Route. If we have no customizations, Ember will provide a default Controller instance for us at run time.
12+
13+
To generate a controller, run
1114
```bash
1215
ember generate controller my-controller-name
1316
```
1417

15-
The value of `my-controller-name` must match the name of the Route that renders
16-
it. So a Route named `blog-post` would have a matching Controller named
17-
`blog-post`.
18+
This creates a controller file at `app/controllers/my-controller-name.js`, and a unit test file at `tests/unit/controllers/my-controller-name-test.js`.
19+
20+
The controller name `my-controller-name` must match the name of the Route that renders it. So if the Route is named `blog-post`, it should have a matching Controller named `blog-post`. The matching file names of the Controller and the Route signals to Ember that this Controller must be used when landing on the `blog-post` Route.
21+
22+
### Where and When to use Controllers?
23+
24+
Controllers are used as an extension of the model loaded from the Route. Any attributes or actions that we want to share with components used on that Route could be defined on the Controller and passed down through the Route’s template.
1825

19-
You only need to generate a Controller if you want to customize its
20-
properties or provide any `actions`. If you have no customizations, Ember will
21-
provide a Controller instance for you at run time.
26+
Controllers are singletons so we should avoid keeping state that does not derive from either the Model or Query Parameters since these would persist in between activations such as when a user leaves the Route and then re-enters it.
2227

23-
Let's explore these concepts using an example of a route displaying a blog
24-
post. Presume a `BlogPost` model that is presented in a `blog-post` template.
28+
Controllers can also contain actions that enable the Route's components to update the Model or Query Parameters through it using Computed Properties.
29+
30+
### Basic Controller Example
31+
32+
Let's explore these concepts using an example of a route displaying a blog post. Assume that the route returns a `BlogPost` model that is presented in the template.
2533

2634
The `BlogPost` model would have properties like:
2735

@@ -30,8 +38,7 @@ The `BlogPost` model would have properties like:
3038
* `body`
3139
* `author`
3240

33-
Your template would bind to these properties in the `blog-post`
34-
template:
41+
In the example below, we can see how the template is using the model properties to display some data.
3542

3643
```handlebars {data-filename=app/templates/blog-post.hbs}
3744
<h1>{{model.title}}</h1>
@@ -46,15 +53,23 @@ template:
4653
</div>
4754
```
4855

49-
In this simple example, we don't have any display-specific properties
50-
or actions just yet. For now, our controller's `model` property acts as a
51-
pass-through (or "proxy") for the model properties. (Remember that
52-
a controller gets the model it represents from its route handler.)
56+
Consider the example where we want to have a controller for a `blog-post` route. In this controller, we are looking to keep track if the user has expanded the body or not.
57+
58+
```javascript {data-filename=app/controllers/blog-post.js}
59+
import Controller from '@ember/controller';
60+
61+
export default Controller.extend({
62+
isExpanded: false,
63+
64+
actions: {
65+
toggleBody() {
66+
this.toggleProperty('isExpanded');
67+
}
68+
}
69+
});
70+
```
5371

54-
Let's say we wanted to add a feature that would allow the user to
55-
toggle the display of the body section. To implement this, we would
56-
first modify our template to show the body only if the value of a
57-
new `isExpanded` property is true.
72+
The property `isExpanded` keeps track if the user has expanded the body or not. The action `toggleBody()` provides a way for the user to provide their setting. Both of the them are used in the updated template below.
5873

5974
```handlebars {data-filename=app/templates/blog-post.hbs}
6075
<h1>{{model.title}}</h1>
@@ -75,27 +90,16 @@ new `isExpanded` property is true.
7590
{{/if}}
7691
```
7792

78-
You can then define what the action does within the `actions` hook
79-
of the controller, as you would with a component:
80-
81-
```javascript {data-filename=app/controllers/blog-post.js}
82-
import Controller from '@ember/controller';
83-
84-
export default Controller.extend({
85-
isExpanded: false,
86-
87-
actions: {
88-
toggleBody() {
89-
this.toggleProperty('isExpanded');
90-
}
91-
}
92-
});
93-
```
93+
We can see that if the property `isExpanded` is toggled to true, we will show the body property of the model to the user. This `isExpanded` is stored in the controller.
9494

9595
### Common questions
9696

97-
<div class="common-question">
98-
<h4>Should I use controllers in my application? I've heard they're going away!</h4>
97+
###### Should we use controllers in my application? I've heard they're going away!
9998

100-
<p>Yes! Controllers are still an integral part of an Ember application architecture, and generated by the framework even if you don't declare a Controller module explicitly.</p>
101-
</div>
99+
Yes! Controllers are still an integral part of an Ember application architecture, and generated by the framework even if you don't declare a Controller module explicitly.
100+
101+
###### When should we create a Controller?
102+
103+
* We want to pass down actions or variables to share with a Route’s child components
104+
* We have a computed property that depends on the results of the model hook
105+
* We need to support query parameters

0 commit comments

Comments
 (0)