New Router API中的路由和资源有什么区别?

the*_*ack 114 ember.js ember-router

我试图理解a Route和a 之间的区别Resource.我理解的方式Resource有助于将Route对象的子路径设置为另一个Route对象.但是当我想到路径的默认名称映射时,我还不清楚.

mav*_*ein 101

请注意,从1.11.0起,this.route仅用于代替this.resource.资料来源:http://guides.emberjs.com/v1.11.0/routing/defining-your-routes/*

看一下这篇文章的详细解释.

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

自从改变资源和路线以来,很多人都对这两者的含义以及它们如何影响命名感到困惑.这是区别:

  • 资源 - 一件事(模特)
  • 路线 - 与事物有关

所以这意味着使用路由和资源的路由器可能如下所示:

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

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

  • PostsRou​​te,PostsController,PostsView
  • PostsIndexRoute,PostsIndexController,PostsIndexView
  • PostsNewRoute,PostsNewController,PostsNewView
  • AnotherRoute,AnotherController,AnotherView

正如我们从这个例子中看到的那样,资源影响正在使用/创建的控制器,路由和视图的命名("新"路由被视为从属于"posts"资源).引用原始来源(我修改了它,因为它很刺激,正如Patrick M在评论中正确指出的那样):

这意味着无论何时创建资源,它都会创建一个全新的命名空间.该命名空间以资源命名,所有子路由都将插入其中.

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

考虑以下具有多个嵌套资源的更复杂的示例:

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" });
});
Run Code Online (Sandbox Code Playgroud)

在这种情况下,资源comments会创建一个全新的命名空间.这意味着在这种情况下得到的路线如下.如您所见,注释资源的Route,Controller和View不以父路由的名称为前缀.这意味着将资源嵌套在另一个资源中会重置命名空间(=创建一个新的命名空间).

  • PostsRou​​te,PostsController,PostsView
  • PostsIndexRoute,PostsIndexController,PostsIndexView
  • PostsNewRoute,PostsNewController,PostsNewView
  • CommentsRou​​te,CommentsController,CommentsView
  • CommentsNewRoute,CommentsNewController,CommentsNewView
  • AnotherRoute,AnotherController,AnotherView

Ember Docs中也解释了这种行为.

  • 这在Ember指南中应该更清楚.起初我对这个概念感到困惑. (4认同)