JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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);
  }
});

大佬总结

以上是大佬教程为你收集整理的Ember.js新路由器:从父动态路由段访问序列化的对象全部内容,希望文章能够帮你解决Ember.js新路由器:从父动态路由段访问序列化的对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:对象