我试图理解Route
和Resource
之间的区别。我理解Resource
的方式有助于将Route
对象的子路径设置到另一个Route
对象。但当我想到路径的默认名称映射时,还不清楚。
最佳答案
Please Note that from 1.11.0 onwards,
this.route
is only used instead ofthis.resource
. Source: http://guides.emberjs.com/v1.11.0/routing/defining-your-routes/*
看看这个 post详细解释。
这是这篇文章的粗略摘要(我做了一些修改):
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:
- resource - a thing (a model)
- route - something to do with the thing
因此,这意味着使用路由和资源的路由器可能如下所示:
App.Router.map(function() {
this.resource("posts", { path: "/" }, function() {
this.route("new", { path: "/new" });
});
this.route("another", { path: "/another" });
});
这将导致创建/使用以下路由:
- PostsRoute、PostsController、PostsView
- PostsIndexRoute、PostsIndexController、PostsIndexView
- PostsNewRoute、PostsNewController、PostsNewView
- 另一个路由、另一个 Controller 、另一个 View
正如我们从这个示例中看到的,资源影响正在使用/创建的 Controller 、路由和 View 的命名("new"路由被视为从属于“帖子”资源)。引用原始来源(我对其进行了修改,因为正如 Patrick M 在评论中正确指出的那样,这很令人恼火):
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.
更新:具有嵌套资源的更复杂示例
考虑以下具有多个嵌套资源的更复杂的示例:
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" });
});
在这种情况下,资源comments
创建了一个全新的命名空间。这意味着在这种情况下生成的路线如下。 正如您所看到的,评论资源的路由、 Controller 和 View 没有以父路由的名称作为前缀。这意味着将一个资源嵌套在另一个资源中会重置命名空间(= 创建一个新的命名空间) .
- PostsRoute、PostsController、PostsView
- PostsIndexRoute、PostsIndexController、PostsIndexView
- PostsNewRoute、PostsNewController、PostsNewView
- CommentsRoute、CommentsController、CommentsView
- CommentsNewRoute、CommentsNewController、CommentsNewView
- 另一个路由、另一个 Controller 、另一个 View
Ember Docs 中也解释了此行为.
关于ember.js - New Router API 中的路由和资源有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14973556/