this.route()和this.resource()有什么区别?

Mar*_*sen 5 javascript ember.js

我知道路由用于将URL映射到模板,但显然你可以用this.route或this.resource定义路由.

App.Router.map(function() {
  this.route("about", { path: "/about" });
  this.route("favorites", { path: "/favs" });
});

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

你是否只想使用this.resource来定义路由的子路由,还是有其他理由我没有得到?

Mik*_*tti 8

你是否只想使用this.resource来定义路由的子路由,还是有其他理由我没有得到?

这是基本的想法.

  • resource = parent(通常是名词)
  • route = child(通常是动词)

还要记住,路由器始终指向当前路由.它无法转换为资源.在幕后,ember自动在每个资源下面生成一个"索引"路径,所以即使你定义了这样的东西:

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

你最终得到这个:

App.Router.map(function() {
  this.resource('about', { path: '/about' }, function() {
    this.route('index');
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 您应该能够这样做 - "contacts"将是/下的资源(例如由ArrayController支持)和":contact_id"将是另一个资源(由ObjectController支持),在其下面有一个"编辑"路由.检查文档中的嵌套资源部分:http://emberjs.com/guides/routing/defining-your-routes/#toc_nested-resources (2认同)