You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
5
4
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.
8
6
9
-
To define a Controller, run:
7
+
A Controller is usually paired with an individual Route of the same name.
10
8
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
11
14
```bash
12
15
ember generate controller my-controller-name
13
16
```
14
17
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.
18
25
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.
22
27
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.
25
33
26
34
The `BlogPost` model would have properties like:
27
35
@@ -30,8 +38,7 @@ The `BlogPost` model would have properties like:
30
38
*`body`
31
39
*`author`
32
40
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.
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.
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.
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.
94
94
95
95
### Common questions
96
96
97
-
<divclass="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!
99
98
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
0 commit comments