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

标签 ember.js ember-router

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

最佳答案

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详细解释。

这是这篇文章的粗略摘要(我做了一些修改):

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" });
});

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

  • PostsRou​​te、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 没有以父路由的名称作为前缀。这意味着将一个资源嵌套在另一个资源中会重置命名空间(= 创建一个新的命名空间) .

  • PostsRou​​te、PostsController、PostsView
  • PostsIndexRoute、PostsIndexController、PostsIndexView
  • PostsNewRoute、PostsNewController、PostsNewView
  • CommentsRou​​te、CommentsController、CommentsView
  • CommentsNewRoute、CommentsNewController、CommentsNewView
  • 另一个路由、另一个 Controller 、另一个 View

Ember Docs 中也解释了此行为.

关于ember.js - New Router API 中的路由和资源有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14973556/

相关文章:

ember.js - 如何在没有 controller.get ('view' 的情况下使操作影响同级 View ?

ember.js - 如何将多个内容生成到 ember.js 组件模板中?

javascript - 全局(窗口)变量在 Emberjs 中行为异常

javascript - 如何将 Packery 与 Ember 一起使用

python - 将 Ember CLI 与 Django 应用集成

javascript - Ember.js - 启用位置历史记录时如何处理 URL 参数?

Ember.js:未捕获的类型错误:无法读取 transitionTo 上未定义的属性 'enter'

ember.js - 动态更新查询参数

ember.js - 用于显示/编辑和创建的相同 Ember.JS 模板

javascript - 错误子状态