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:
ember g route post
ember g route post/comments
ember g model post
For example, the following routing settings:
// router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('blog-post');
this.route('post', { path: '/posts/:post_id' }, function() {
this.route('comments');
});
});
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
// app/controllers/comments.js
import Ember from 'ember';
export default Ember.Controller.extend({
postController: Ember.inject.controller('post')
});
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
// app/controllers/comments.js
import Ember from 'ember';
export default Ember.Controller.extend({
postController: Ember.inject.controller('post'),
post: Ember.computed.reads('postController.model')
});
Finally, you can display
comment
the model post and the comment
post
comment
template.
<h1>Comments for {{post.title}}</h1>
<ul>
{{#each model as |comment|}}
<li>{{comment.text}}</li>
{{/each}}
</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!!