Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js New Router: accessing serialized objects from parent dynamic route segments

Tags:

ember.js

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.
like image 793
Sammy Avatar asked Jan 05 '13 16:01

Sammy


1 Answers

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);
  }
});
like image 137
Mike Grassotti Avatar answered Sep 30 '22 06:09

Mike Grassotti