新路由器API中的路由和资源有什么区别? [英] What is the difference between a route and resource in New Router API?

查看:112
本文介绍了新路由器API中的路由和资源有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 Route 资源之间的区别。我理解资源的方式有助于将 Route 对象的子路径设置为另一个 Route Object。但是,当我想到路径的默认名称映射也是不清楚的。

I am trying to understand the difference between a Route and a Resource. The way I understand Resource helps to set sub paths of a Route object to another Route Object. But its unclear when i think of default name mapping happening for paths as well.

推荐答案


请注意,从1.11.0开始,仅使用 this.route 而不是 this.resource 。资料来源: http://guides.emberjs.com/v1.11.0/routing/defining-your -routes / *

Please Note that from 1.11.0 onwards, this.route is only used instead of this.resource. Source: http://guides.emberjs.com/v1.11.0/routing/defining-your-routes/*

看看这个 post 详细说明。

这是这篇文章的粗略摘要(我已经修改了一下):

This is a rough summary of this post (i have modified a bit):


自从更改资源以来而路由很多人都是
混淆了两者的含义,以及它们如何影响命名。
这里有区别:

Ever since the change to resource and route a lot of people are confused about the meaning of the two and how they affect naming. Here’s the difference:


  • 资源 - 一个东西(一个模型)

  • - 与事情有关的事情

所以这意味着使用路由和资源的路由器可能会看像这样:

So this means a router using a route and resource might look like this:

App.Router.map(function() {
  this.resource("posts", { path: "/" }, function() {
    this.route("new", { path: "/new" });
  });
  this.route("another", { path: "/another" });
});

这将导致以下路由被创建/使用:

This would result in the following routes being created/used:


  • PostsRou​​te,PostsController,PostsView

  • PostsIndexRoute,PostsIndexController,PostsIndexView

  • PostsNewRoute,PostsNewController,PostsNewView

  • AnotherRoute,AnotherController,AnotherView

  • PostsRoute, PostsController, PostsView
  • PostsIndexRoute, PostsIndexController, PostsIndexView
  • PostsNewRoute, PostsNewController, PostsNewView
  • AnotherRoute, AnotherController, AnotherView

从这个例子我们看到,资源影响命名使用/创建的控制器,路由和视图(新路由被视为从属于帖子资源)。引用原始来源(我修改它,因为它是令人讨厌的,因为Patrick M在评论中正确地指出):

As we see from this example, resource effect the naming of the Controllers,Routes and Views being used/created (The "new" route is treated as subordinate to "posts" resource). Cite from the original source (i modified it, because it was irritating as Patrick M correctly pointed out in the comments):


这意味着每当你创建一个资源,它将创建一个全新的
命名空间。该命名空间以
资源命名,并且所有子路由都将被插入到其中。

This means whenever you create a resource it will create a brand new namespace. That namespace is named after the resource and all of the child routes will be inserted into it.

更新:更复杂的嵌套资源示例

考虑以下更复杂的示例,其中包含多个嵌套资源:

Consider the following more complex example with multiple nested resources:

App.Router.map(function() {
  this.resource("posts", { path: "/" }, function() {
    this.route("new", { path: "/new" });
    this.resource("comments", { path: "/comments" }, function() {
      this.route("new", { path: "/new" });
    });
  });
  this.route("another", { path: "/another" });
});

在这种情况下,资源注释创建一个全新的命名空间。这意味着在这种情况下结果路由将是以下。 正如您可以看到,注释资源的路由,控制器和视图不以父路由的名称为前缀。这意味着在另一个资源中嵌套资源将重置命名空间(=创建一个新的命名空间) 。

In this case the resource comments creates a brand new namespace. This means the resulting routes in this case will be the following. As you can see the Route, Controller and View for the comments resource are not prefixed with the name of the parent route. That means nesting a resource within another resource resets the namespace (= creates a new namespace).


  • PostsRou​​te,PostsController,PostsView

  • PostsIndexRoute,PostsIndexController,PostsIndexView

  • PostsNewRoute,PostsNewController,PostsNewView

  • CommentsRou​​te,CommentsController,CommentsView

  • 评论NewRoute,CommentsNewController ,CommentsNewView

  • AnotherRoute,AnotherController,AnotherView

  • PostsRoute, PostsController, PostsView
  • PostsIndexRoute, PostsIndexController, PostsIndexView
  • PostsNewRoute, PostsNewController, PostsNewView
  • CommentsRoute, CommentsController, CommentsView
  • CommentsNewRoute, CommentsNewController, CommentsNewView
  • AnotherRoute, AnotherController, AnotherView

Ember Docs 中。

这篇关于新路由器API中的路由和资源有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆