Ember.js API (v2.11.0) 翻译 ---004.Routing---002---Defining Your Routes Edit Page(定义你的路由)



Defining Your Routes Edit Page
定义你自己的路由


When your application starts, the router matches the current URL to the routes that you've defined. The routes, in turn, are responsible for displaying templates, loading data, and setting up application state.
当应用启动是,路由器会根据当前的URL去匹配对应已经定义的路由,然后依次是显示模板,加载数据,然后建立应用程序状态


To define a route, run
定义一个路由
1
ember generate route route-name
This creates a route file at app/routes/route-name.js, a template for the route at app/templates/route-name.hbs, and a unit test file at tests/unit/routes/route-name-test.js. It also adds the route to the router.


Basic Routes


The map() method of your Ember application's router can be invoked to define URL mappings. When calling map(), you should pass a function that will be invoked with the value this set to an object which you can use to create routes.


app/router.js
1
2
3
4
Router.map(function() {
  this.route('about', { path: '/about' });
  this.route('favorites', { path: '/favs' });
});
Now, when the user visits /about, Ember will render the about template. Visiting /favs will render the favorites template.


You can leave off the path if it is the same as the route name. In this case, the following is equivalent to the above example:


app/router.js
1
2
3
4
Router.map(function() {
  this.route('about');
  this.route('favorites', { path: '/favs' });
});
Inside your templates, you can use {{link-to}} to navigate between routes, using the name that you provided to the route method.


1
2
3
4
5
6
{{#link-to "index"}}<img class="logo">{{/link-to}}


<nav>
  {{#link-to "about"}}About{{/link-to}}
  {{#link-to "favorites"}}Favorites{{/link-to}}
</nav>
The {{link-to}} helper will also add an active class to the link that points to the currently active route.


Multi-word route names are conventionally dasherized, such as:


app/router.js
1
2
3
Router.map(function() {
  this.route('blog-post', { path: '/blog-post' });
});
The route defined above will by default use the blog-post.js route handler, the blog-post.hbs template, and be referred to as blog-post in any {{link-to}} helpers.


Multi-word route names that break this convention, such as:


app/router.js
1
2
3
Router.map(function() {
  this.route('blog_post', { path: '/blog-post' });
});
will still by default use the blog-post.js route handler and the blog-post.hbs template, but will be referred to as blog_post in any {{link-to}} helpers.


Nested Routes


Often you'll want to have a template that displays inside another template. For example, in a blogging application, instead of going from a list of blog posts to creating a new post, you might want to have the post creation page display next to the list.


In these cases, you can use nested routes to display one template inside of another.


You can define nested routes by passing a callback to this.route:


app/router.js
1
2
3
4
5
Router.map(function() {
  this.route('posts', function() {
    this.route('new');
  });
});
Assuming you have already generated the posts route, to generate the above nested route you would run:


1
ember generate route posts/new
And then add the {{outlet}} helper to your template where you want the nested template to display:


templates/posts.hbs
1
2
3
<h1>Posts</h1>
<!-- Display posts and other content -->
{{outlet}}
This router creates a route for /posts and for /posts/new. When a user visits /posts, they'll simply see the posts.hbs template. (Below, index routes explains an important addition to this.) When the user visits posts/new, they'll see the posts/new.hbs template rendered into the {{outlet}} of the posts template.


A nested route's names includes the names of its ancestors. If you want to transition to a route (either via transitionTo or {{#link-to}}), make sure to use the full route name (posts.new, not new).


The application route


The application route is entered when your app first boots up. Like other routes, it will load a template with the same name (application in this case) by default. You should put your header, footer, and any other decorative content here. All other routes will render their templates into the application.hbs template's {{outlet}}.


This route is part of every application, so you don't need to specify it in your app/router.js.


Index Routes


At every level of nesting (including the top level), Ember automatically provides a route for the / path named index. To see when a new level of nesting occurs, check the router, whenever you see a function, that's a new level.


For example, if you write a simple router like this:


app/router.js
1
2
3
Router.map(function(){
  this.route('favorites');
});
It is the equivalent of:


app/router.js
1
2
3
4
Router.map(function(){
  this.route('index', { path: '/' });
  this.route('favorites');
});
The index template will be rendered into the {{outlet}} in the application template. If the user navigates to /favorites, Ember will replace the index template with the favorites template.


A nested router like this:


app/router.js
1
2
3
4
5
Router.map(function() {
  this.route('posts', function() {
    this.route('favorites');
  });
});
Is the equivalent of:


app/router.js
1
2
3
4
5
6
7
Router.map(function(){
  this.route('index', { path: '/' });
  this.route('posts', function() {
    this.route('index', { path: '/' });
    this.route('favorites');
  });
});
If the user navigates to /posts, the current route will be posts.index, and the posts/index template will be rendered into the {{outlet}} in the posts template.


If the user then navigates to /posts/favorites, Ember will replace the {{outlet}} in the posts template with the posts/favorites template.


Dynamic Segments


One of the responsibilities of a route is to load a model.


For example, if we have the route this.route('posts');, our route might load all of the blog posts for the app.


Because /posts represents a fixed model, we don't need any additional information to know what to retrieve. However, if we want a route to represent a single post, we would not want to have to hardcode every possible post into the router.


Enter dynamic segments.


A dynamic segment is a portion of a URL that starts with a : and is followed by an identifier.


app/router.js
1
2
3
4
Router.map(function() {
  this.route('posts');
  this.route('post', { path: '/post/:post_id' });
});
If the user navigates to /post/5, the route will then have the post_id of 5 to use to load the correct post. Ember follows the convention of :model-name_id for two reasons. The first reason is that Routes know how to fetch the right model by default, if you follow the convention. The second is that params is an object, and can only have one value associated with a key. To put it in code, the following will not work properly:


app/router.js
1
2
3
4
5
Router.map(function() {
  this.route('photo', { path: '/photo/:id' }, function() {
    this.route('comment', { path: '/comment/:id' });
  });
});
But the following will:


app/router.js
1
2
3
4
5
Router.map(function() {
  this.route('photo', { path: '/photo/:photo_id' }, function() {
    this.route('comment', { path: '/comment/:comment_id' });
  });
});
In the next section, Specifying a Route's Model, you will learn more about how to load a model.


Wildcard / globbing routes


You can define wildcard routes that will match multiple URL segments. This could be used, for example, if you'd like a catch-all route which is useful when the user enters an incorrect URL not managed by your app. Wildcard routes begin with an asterisk.


app/router.js
1
2
3
Router.map(function() {
  this.route('not-found', { path: '/*path' });
});
app/templates/not-found.hbs
1
<p>Oops, the page you're looking for wasn't found</p>
In the above example we have successfully used a wildcard route to handle all routes not managed by our application so that when a user navigates to /a/non-existent/path they will be shown a message that says the page they're looking for wasn't found.


Route Handlers


To have your route do something beyond render a template with the same name, you'll need to create a route handler. The following guides will explore the different features of route handlers. For more information on routes, see the API documentation for the router and for route handlers.


Introduction
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值