Ember.js新路由器:从父动态路由段访问序列化的对象 [英] Ember.js New Router: accessing serialized objects from parent dynamic route segments

查看:64
本文介绍了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 b $ b match('/')。to('commentsIndex');
match('/:comment_id')to('showComment');
});
}) ;
});
});

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



为什么 CommentsRou​​te#model(params) CommentsIndexRoute 参数始终为空?如何检索发布的评论?



我的小提琴



运行示例(有控制台日志显示问题。





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



对于模型具有复合键的应用程序,这是不可接受的,如果我们转换到 showComment 一步一步,我们可以获得评论实例:

 code> App.ShowCommentRoute = Ember.Route.extend({
model:function(params){
var post_id = this.controllerFor('post')。get('content.id' );
return App.Comment.find(post_id,pa rams.comment_id);
}
});

但是,如果我们直接访问 / posts / 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);
}
});


There was a similar issue already.

Assuming the following routes:

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

Is it possible to access both post_id and comment_id in ShowCommentRoute? Otherwise should I forget about composite keys in my models?

Why CommentsRoute#model(params) and CommentsIndexRoute argument is always empty? How to retrieve Post's comments when?

My fiddle.

Run this example too (there are console log showing the problem.

UPDATE after some investigation:

Only PostRoute will have params.post_id. Only ShowCommentRoute will have params.comment_id and will not have params.post_id.

This is unacceptable for applications where models have composite keys. In case when we are transitioning to showComment step by step, we can obtain Comment instance:

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

But this won't work if we directly visiting /posts/1/comments/1. In this case this.controllerFor('post') always undefined.

  • If you have nested routes with dynamic segments you can not access this segments in *IndexRoute (PostRoute and PostInderRoute in this example)
  • Shortly, it's impossible to obtain parent route model when directly visiting nested route.

解决方案

Using ember-1.0.0-rc.1 it is now possible to access the parent route's model when directly visiting a 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新路由器:从父动态路由段访问序列化的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆