Idiomatic way to handle nested objects?

Note: this is a repost of {{control}} vs itemController. I think the title I chose was misleading. It looked like a technical question regarding a yet to be finalized feature. It is not. It is actually (I think) fairly generic.

If you use Ember to front end a relational DB based back end, there is a good chance you need to deal with tons of nested objects.

It seems to me not all objects in a hierarchy need their own controller. Some do, some don’t, some just need an ArrayController.

Is there a conventional/idiomatic way to handle this? Could you guys share recommendations?

If we consider a simple example:

 Post hasMany Comments
 Author belongsTo Comment

How do you render a Post with its “descendants” (if there can be a “standard” approach)?

My first take was:

… post template...
{{#each comments}}
 {{ partial 'comment' }} // Say I don't need a controller for each comment
{{/each}}



… comment template...
// If you need a controller for your author, how do you handle this? 
// {{ partial }} won't help, and {{ control }} won't work.

It seems like a fairly standard use case to me. So the fact I am stuck make me think I am doing something wrong. To quote the PeepCode screencast:

“If you find that something is difficult to do [in Ember] or requires significant configuration away from the defaults, it should be a signal to you that you might be going in the wrong direction.”

Could you guys shed some light on this?

Thank you!

2 Likes

This is a great post for StackOverflow. It would be great if you could repost it there. Thanks!

This topic is now invisible. It will no longer be displayed in any topic lists. The only way to access this topic is via direct link.

This topic is now visible. It will be displayed in topic lists.

Upon further consideration, I’ve decided to reopen this. It seems that this issue may merit a little more discussion that SO generally affords.

As @wycats said here you can actually use {{render "comment" this}} multiple times. It only uses a singleton controller when you don’t give it a model.

You can also use that in the comment template, but this also brings a question. Why do you want to extract the author logic from the comment at all? If I imagine something like Discourse and it’s replies

I don’t think the author box is reusable, since it always looks a bit different in different parts of the site.

But maybe I’m missing something here? :slight_smile:

Thanks @darthdeus. The post/comment/author example I gave was just theoretical - and probably a bit too simplistic…

But your comment made me think:

I don’t think the author box is reusable, since it always looks a bit different in different parts of the site.

My rule of thumb so far has been: if I want to give to the user the ability to apply certain actions to the representation of a model, it needs to get its own controller, independently of how the model “looks” in different parts of the app.

If we still use the same example: let’s say I want to give the ability to “follow” an author. It should be possible whether the author is shown within a comment, or separated.

With my current “rule”, it would mean I would want my author object to have its own controller all the time.

But am I going in the wrong direction with this? Should it be approached differently in the context of Ember?

Thanks! PJ

What is the conclusion of this discussion? I am facing similar issues with the lack of support for child controllers in Ember, or am I approaching this problem in the wrong direction?