微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/js/152763.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐