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

Ember manages the dependencies between controllers


May 09, 2021 Ember.js Reference documents



In the case of route nesting, you may need to communicate between two different controller Prepare according to the usual practice:

  1. ember g route post
  2. ember g route post/comments
  3. ember g model post

For example, the following routing settings:

  1. // router.js
  2. import Ember from 'ember';
  3. import config from './config/environment';
  4. var Router = Ember.Router.extend({
  5. location: config.locationType
  6. });
  7. Router.map(function() {
  8. this.route('blog-post');
  9. this.route('post', { path: '/posts/:post_id' }, function() {
  10. this.route('comments');
  11. });
  12. });
  13. export default Router;

For the routing table generated by this routing configuration, see Ember.js the 13th assistant of the Getting Started Guide.

If the user /posts/1/comments T he post is postController and not directly commentsController And then what if you post display a comment comment a post?

To do this, you can postController commentController

  1. // app/controllers/comments.js
  2. import Ember from 'ember';
  3. export default Ember.Controller.extend({
  4. postController: Ember.inject.controller('post')
  5. });

Once the comments is accessed, postController gets the model which model read-only. In order to get the model post you also need to add a postController.model

  1. // app/controllers/comments.js
  2. import Ember from 'ember';
  3. export default Ember.Controller.extend({
  4. postController: Ember.inject.controller('post'),
  5. post: Ember.computed.reads('postController.model')
  6. });

Finally, you can display comment the model post and the comment post comment template.

  1. <h1>Comments for {{post.title}}</h1>
  2. <ul>
  3. {{#each model as |comment|}}
  4. <li>{{comment.text}}</li>
  5. {{/each}}
  6. </ul>

For more aliasing, move here to see the introduction to the API documentation. If you want to learn more about injection, see the tutorial here (the new version of the official website no longer has the documentation for this address).

controller contents of this chapter of Controller have been fully covered here, and there are only a few 2 tutorials, which controller will be replaced by components in future versions of Ember.

So the next chapter will introduce the model to everyone, the model for Ember is a very important piece of content, content is more! I 'll use 9 articles to introduce you to the model, from definition to its use and so on.
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!!