【问题标题】:Proper way to define a resource routes in Ember?在 Ember 中定义资源路由的正确方法?
【发布时间】:2013-02-09 11:50:10
【问题描述】:

在指南中,我可以看到 2 种方法来定义资源的路线,我想知道我应该使用哪种方法,为什么?


在这里找到:http://emberjs.com/guides/routing/defining-your-routes/

App.Router.map(function() {
  this.resource('posts');
  this.resource('post', { path: '/posts/:post_id' });
});

在这里找到:http://emberjs.com/guides/templates/links/

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

我发现必须定义两个资源 postspost 很奇怪,而实际上它只是一个 Post 资源。

【问题讨论】:

  • 那篇文章在“当一个路由/资源”darthdeus.github.com/blog/2013/02/01/…987654323@
  • 我实际上是在几天前阅读了整个博客,但由于我自己没有经历过,所以我忘记了。这实际上很好地回答了我的问题。如果您遵循 ember 指南或本文中给出的定义,您不会说第二个示例不“正确”吗?不知道他们为什么使用路由发布。也许只是为了举例。如果需要,请将此链接添加为答案。

标签: routes ember.js


【解决方案1】:

我相信当你这样做时

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

它在帖子模板的 {{outlet}} 内呈现帖子模板..

当你这样做时

App.Router.map(function() {
  this.resource('posts');
  this.resource('post', { path: '/posts/:post_id' });
});

当您访问 /posts/:post_id 时,帖子模板不会呈现

【讨论】:

  • 是的,你是对的。为什么他们在您的第一个示例中使用路由而不是资源来“发布”?
  • 来自emberjs.com/guides/routing/defining-your-routes :您应该将 this.resource 用于表示名词的 URL,并将 this.route 用于表示修饰这些名词的形容词或动词的 URL。
  • 一个叫做posts的资源实际上创建了两个路由,PostsRou​​te和PostsIndexRoute。 PostsIndexRoute 在 PostsRou​​te 出口内呈现
猜你喜欢
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 2011-01-17
相关资源
最近更新 更多