Ember.js新路由器:从父动态路由段访问序列化对象

Sam*_*mmy 10 ember.js

还有一个类似的问题.

假设以下路线:

App.Router.map(function (match) {
  match('/').to('index');
  match('/posts').to('posts', function (match) {
    match('/').to('postsIndex');
    match('/:post_id').to('post', function (match) {
      match('/comments').to('comments', function (match) {
        match('/').to('commentsIndex');
        match('/:comment_id').to('showComment');
      });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

是否可以访问post_idcomment_id进入ShowCommentRoute?否则我应该忘记模型中的复合键?

为什么CommentsRoute#model(params)CommentsIndexRoute论点总是空的?如何在Post何时检索评论?

我的小提琴.

也运行此示例(有控制台日志显示问题.

经过一番调查后更新:

只会PostRouteparams.post_id.只会ShowCommentRouteparams.comment_id和不会有params.post_id.

对于模型具有复合键的应用程序,这是不可接受的.如果我们showComment一步一步过渡,我们可以获得Comment实例:

App.ShowCommentRoute = Ember.Route.extend({
  model: function(params) {
    var post_id = this.controllerFor('post').get('content.id');
    return App.Comment.find(post_id, params.comment_id);
  }
});
Run Code Online (Sandbox Code Playgroud)

但如果我们直接访问,这将无效/posts/1/comments/1.this.controllerFor('post')总是在这种情况下undefined.

  • 如果您有嵌套的动态段的路线,你不能访问该段中*IndexRoute(PostRoute以及PostInderRoute在这个例子中)
  • 很简单,直接访问嵌套路由时无法获得父路径模型.

Mik*_*tti 13

使用ember-1.0.0-rc.1,现在可以在直接访问url时访问父路由的模型.

App.ShowCommentRoute = Ember.Route.extend({
  model: function(params) {
    var post = this.modelFor('post');
    return App.Comment.find(post.get('id'), params.comment_id);
  }
});
Run Code Online (Sandbox Code Playgroud)