Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: accessing serialized objects from parent dynamic route segments #1334

Closed
leepfrog opened this issue Aug 30, 2012 · 9 comments
Closed

Comments

@leepfrog
Copy link

Creating this issue to open up discussion on the topic...

given a route /:post_id,
a subroute /view,
a subroute /:comment_id

I want the ability to access the relevant post object from connectOutlets for the /:comment_id subroute.

I realize that you can simply set the post object on a controller as part of connectOutlets for :post_id, but it seems sort of odd to obtain that object through a controller instead of through the router directly.

This is one option:

connectOutlets = function (router,context, contexts) { .. }
// contexts.get('post') => App.Post<Object#1>

I picture deserialize being responsible for setting this up for the routes in some way.

As a work around, @kselden came up with a good solution that involves using ObjectControllers for url data pieces. See: http://jsfiddle.net/krisselden/uErrd/32/

@leepfrog
Copy link
Author

An example of this from rails:

# Rails route definition 
  resources :posts do
    resources :comments
  end

# URL
  # /posts/:post_id/comments/:id(.:format)      comments#show

# Data passed to controller
  # /posts/1/comments/15
  # params = {:post_id => 1, :id => 15}

This would be slightly different for ember. I would expect to get back Post.find(1) and Comment.find(15) as objects.

@rlivsey
Copy link
Contributor

rlivsey commented Oct 2, 2012

Related to this, I've found I sometimes need access to parent routes deserialized content in deserialize of a nested route.

For example: http://jsfiddle.net/rlivsey/vg4ga/

Here we have /projects/:project_id/posts/:post_id

In the post action we need to do a request to the server including the project ID and the post ID, but we don't have access to the project from within deserialize.

As all deserialize's are called before any connectOutlets we can't access the project with router.get("projectsController.content") as that hasn't been setup yet.

In my app I'm currently getting around this by setting the content property in the parent's deserialize which the child can access, but this feels rather wrong as it's then happening twice as it's then set again in connectOutlets.

@workmanw
Copy link

workmanw commented Oct 8, 2012

Just a thought, but generally I would expect the parent route's deserialized content to be available on it's corresponding controller is one form or another. That's how we access it on our app. I'm not necessarily expecting that to hold true for all apps, but i'm guessing it would for most.

@wagenet
Copy link
Member

wagenet commented Oct 20, 2012

@leepfrog, @kselden, can you explain why @workmanw's suggestion isn't sufficient?

@caligo-mentis
Copy link
Contributor

I also ran into this problem. For example there is two routes:

/:parent_id
/:parent_id/:child_id

To obtain model from child_id need to know the parent_id.

Of course I can do something like /:parent_id_and_child_id instead of /:parent_id/:child_id but this is not quite right solution.

@wildchild
Copy link

@wagenet, as @rlivsey said, deserialize of child route runs before connectOutlets of parent route. The workaround is to assign controller's data in deserialize of parent route looking weird, because connectOutlets will also load data. Yeah, there could be a check for data already loaded, but why not simply pass parent dynamic segments to children just like Rails does, that's what developers assume by default.

@wagenet
Copy link
Member

wagenet commented Jan 1, 2013

@leepfrog I'm assuming this doesn't directly apply to the new router. If you still have issues, please file a new ticket.

@wagenet wagenet closed this as completed Jan 1, 2013
@nragaz
Copy link
Contributor

nragaz commented Jan 10, 2013

FWIW, this is still an issue with the new router. A workaround is to override serialize on the parent route so that it sets the parent route's currentModel to the model that is passed for serialization. I don't think this should have any side effects and I wonder if it should be the default behaviour?

UPDATE: ugh, I see the problem here - serialize is called when generating a URL, which may not be the current model. And in any case, this mysteriously isn't working for the original use case now that it's morning. Sorry.

@eckardt
Copy link

eckardt commented Jan 19, 2013

With Routing v2, what looks like the right way to do this is to query the parent route's model parameters in the subroute's model hook using modelFor.

App.CommentRoute = Ember.Route.extend({
  model: function(params) {
    var post = this.modelFor('post');
    return App.Comment.find({post_id: post.get('id'), comment_id: params.comment_id});
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants