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

已经有一个类似的 issue.

假设以下路线:

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

是否可以访问ShowCommentRoute中的post_id和comment_id?否则我应该忘记我的模型中的复合键?

为什么CommentRoute#model(params)和CommentsIndexRoute参数始终为空?何时检索帖子的评论

我的fiddle.

运行这个example(有控制台日志显示问题.

更新后经过一番调查:

只有PostRoute将有params.post_id.
只有ShowCommentRoute将具有params.comment_id,并且不会有params.post_id.

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

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

但是如果我们直接访问/帖子/ 1 /评论/ 1,这不行.在这种情况下,this.controllerFor(‘post’)总是未定义.

>如果您有嵌套的路径与动态段,您不能访问这个段在* IndexRoute(在这个例子中的PostRoute和PostInderRoute)
>很快,在直接访问嵌套路由时,不可能获得父路由模型.

解决方法

使用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);
  }
});

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...