Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ember controller


May 09, 2021 Ember.js Reference documents


Table of contents


Starting with this article and entering Chapter 5 controllers, controller are becoming more streamlined and have more single responsibilities at E mber2.0 -- processing logic.

Here's the preparation. Re-create an Ember project and still use the Ember CLI command to create it.

  1. ember new chapter5_controllers
  2. cd chapter5_controllers
  3. ember server

When you execute a project in your browser, you see the following information to indicate that the project was successfully built. Welcome to Ember

1. Introduction to the controller

The controller is very similar to the component, so it is likely that the component will completely replace the controller in a new release in the future, and it is likely that Ember will exit ember as the updated controller in the Ember version. The components in the current version are not yet accessible directly through routing and need to be called by templates to use the controller may actually exit Ember by then!

That's why controllers are rarely used in modular Ember controller Even with controller there are two things you can do:

  1. controller primarily designed to maintain the current routing state. In general, the properties of the model are saved to the server, but controller of the controller are not saved to the server.
  2. The action on the component needs to controller the controller layer to route layer.

The rendering of the template context is controller route of the current controller. Ember follows the philosophy that "convention is better than configuration," which means that if you controller you create one, not everything for "work-friendly."

The following example is the demo route display blog post Suppose the blog-post used to present blog-post and contains the following id the model not need to be id the model):

  • title
  • intro
  • body
  • author

model defined as follows:

  1. // app/models/blog-post.js
  2. import DS from 'ember-data';
  3. export default DS.Model.extend({
  4. title: DS.attr('string'), // 属性默认为string类型,可以不指定
  5. intro: DS.attr('string'),
  6. body: DS.attr('string'),
  7. author: DS.attr('string')
  8. });

Add route at the route layer and return a model object model

  1. // app/routes/blog-post.js
  2. import Ember from 'ember';
  3. export default Ember.Route.extend({
  4. model: function() {
  5. var blogPost = this.store.createRecord('blog-post', {
  6. title: 'DEFINING A COMPONENT', // 属性默认为string类型,可以不指定
  7. intro: "Components must have at least one dash in their name. ",
  8. body: "Components must have at least one dash in their name. So blog-post is an acceptable name, and so is audio-player-controls, but post is not. This prevents clashes with current or future HTML element names, aligns Ember components with the W3C Custom Elements spec, and ensures Ember detects the components automatically.",
  9. author: 'ubuntuvim'
  10. });
  11. // 直接返回一个model,或者你可以返回promises,
  12. return blogPost;
  13. }
  14. });

The template for displaying information is as follows:

  1. <h1>{{model.title}}</h1>
  2. <h2>{{model.author}}</h2>
  3. <div class="intro">
  4. {{model.intro}}
  5. </div>
  6. <hr>
  7. <div class="body">
  8. {{model.body}}
  9. </div>

If your code is not written incorrectly, you will also get the following results:

Ember controller

Welcome to Ember is the information for the main template, {{outlet}} which you application.hbs but remember not to delete the information, otherwise nothing will be displayed.

In this example, no specific property or specified action is action A t this point, the role of the controller's model property is simply the pass-through of the model property. Note: The model obtained by model is route route.

Here's an added feature to this example: Users can click on the title to trigger the display or hide post the post. With a property isExpanded code for the template and controller is modified separately below.

  1. // app/controllers/blog-post.js
  2. import Ember from 'ember';
  3. export default Ember.Controller.extend({
  4. isExpanded: false, //默认不显示body
  5. actions: {
  6. toggleBody: function() {
  7. this.toggleProperty('isExpanded');
  8. }
  9. }
  10. });

Add controller property to the isExpanded which is also controller you do not define this property in the controller. For an controller this controller code, see Ember.js the 15th (action) assistant of the Getting Started Guide.

  1. <h1>{{model.title}}</h1>
  2. <h2>{{model.author}}</h2>
  3. <div class="intro">
  4. {{model.intro}}
  5. </div>
  6. <hr>
  7. {{#if isExpanded}}
  8. <button {{action 'toggleBody'}}>hide body</button>
  9. <div class="body">
  10. {{model.body}}
  11. </div>
  12. {{else}}
  13. <button {{action 'toggleBody'}}>Show body</button>
  14. {{/if}}

Use the if assistant in the template to determine the value isExpanded and true is body otherwise it is not displayed.

The result after the page loads is as follows, body content is not displayed, the button "Show body" is clicked to display the content, and the button becomes "hide body". Then click this button and the body content will body displayed.

Ember controller

Ember controller

By this controller responsibilities you should have a general understanding, the main role is logical judgment, processing, such as in this example body content, in fact, you can also put the controller code in the controller class route class can also achieve this model to return as a model isExpanded model model processing), please let the reader do it themselves, but put the logic route will make the route route single", route main responsibility is to initialize the data. I think that's one of the controller still keeps controllers!!


The full code of the blog post is placed in Github (the blog post has been modified several times, and the code on the blog post may be different from the github code, but the impact is small!). I f you think the blog post is a bit useful to you, please give me a star on the star project. Yours is definitely the biggest motivation for me!!