简体   繁体   English

ember link在路线的模型钩子中发送空的参数

[英]ember linkTo sending empty params in route's model hook

I have a blog site where going to the url /posts should list all posts. 我有一个博客网站,去url /posts应列出所有帖子。 Then clicking a link for /posts/:post_id should display the details of that post. 然后单击/posts/:post_id的链接应显示该帖子的详细信息。 However, when clicking the link, it seems that the linkTo is not passing the :post_id properly. 但是,单击链接时,似乎linkTo没有正确传递:post_id Here is my code: 这是我的代码:

Router: 路由器:

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

  this.route("posts.index", { path: "/" });//Links root url to posts index template
});

Routes: 路线:

/****** Posts ******/
App.PostsRoute = Ember.Route.extend({

});

App.PostsIndexRoute = Ember.Route.extend({
  model: function() {
    return App.Post.find();
  }
});

/****** Post *******/
App.PostRoute = Ember.Route.extend({

});

App.PostIndexRoute = Ember.Route.extend({

});

Controllers: 控制器:

/****** Posts ******/
App.PostsController = Ember.ArrayController.extend({

});

App.PostsIndexController = Ember.ArrayController.extend({

});


/****** Post ******/
App.PostController = Ember.ObjectController.extend({

});

App.PostIndexController = Ember.ObjectController.extend({

});

Posts/Index Template: 帖子/索引模板:

{{#each post in controller}}
  <tr>
    <td>{{post.name}}</td>
    <td>{{post.title}}</td>
    <td>{{post.content}}</td>
    <td>{{#linkTo post.index post}}View{{/linkTo}}</td>
  </tr>
{{/each}}

Every time I click the {{linkTo post.index post}} , it takes me to the correct url, renders the correct templates (post/index), but nothing displays. 每次我点击{{linkTo post.index post}} ,它会将我带到正确的网址,呈现正确的模板(帖子/索引),但不会显示任何内容。 I tried putting a model hook in the PostIndexRoute like this: 我尝试在PostIndexRoute中放置一个模型钩子,如下所示:

App.PostIndexRoute = Ember.Route.extend({
  model: function(params) {
    return App.Post.find(params.post_id);
  }
});

But params = {} It has no data in it. 但params = {}它没有数据。 Can someone point me in the right direction? 有人能指出我正确的方向吗? what am I doing wrong? 我究竟做错了什么?

Since you are passing the post object you need to {{linkTo}} a route that accepts a dynamic URL segment. 由于您要传递post对象,因此需要{{linkTo}}接受动态URL段的路由。 In your example posts.index doesn't accept a parameter. 在您的示例中, posts.index不接受参数。

<td>{{#linkTo post post}}View{{/linkTo}}</td>

Here is an example of a way to structure your code to better handle the routing: JSBin example 下面是一个构造代码以更好地处理路由的方法示例JSBin示例

You should also check out the Ember.js routing guides as an example of how to structure routes. 您还应该查看Ember.js路由指南,作为如何构建路由的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM