# Idiomatic way to handle nested objects?

**URL:** <https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527>\
**Category:** Uncategorized\
**Created:** [March 19, 2013, 10:05am UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527 "2013-03-19T10:05:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![thingista](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@thingista](https://discuss.emberjs.com/u/thingista)\
**Post date:** [March 19, 2013, 10:05am UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/1 "2013-03-19T10:05:34Z")

</div>

_Note: this is a repost of [{{control}} vs itemController](http://discuss.emberjs.com/t/control-vs-itemcontroller/493). 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!

---

<div class="post-metadata">

**Author:** ![pwagenet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pwagenet/32/14172_2.png) [@pwagenet](https://discuss.emberjs.com/u/pwagenet)\
**Post date:** [March 20, 2013, 4:22pm UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/2 "2013-03-20T16:22:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pwagenet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pwagenet/32/14172_2.png) [@pwagenet](https://discuss.emberjs.com/u/pwagenet)\
**Post date:** [March 20, 2013, 4:22pm UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/3 "2013-03-20T16:22:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![pwagenet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pwagenet/32/14172_2.png) [@pwagenet](https://discuss.emberjs.com/u/pwagenet)\
**Post date:** [March 20, 2013, 4:37pm UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/4 "2013-03-20T16:37:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pwagenet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pwagenet/32/14172_2.png) [@pwagenet](https://discuss.emberjs.com/u/pwagenet)\
**Post date:** [March 23, 2013, 2:54am UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/5 "2013-03-23T02:54:59Z")

</div>



---

<div class="post-metadata">

**Author:** ![pwagenet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pwagenet/32/14172_2.png) [@pwagenet](https://discuss.emberjs.com/u/pwagenet)\
**Post date:** [March 23, 2013, 2:55am UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/6 "2013-03-23T02:55:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pwagenet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pwagenet/32/14172_2.png) [@pwagenet](https://discuss.emberjs.com/u/pwagenet)\
**Post date:** [March 23, 2013, 2:55am UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/7 "2013-03-23T02:55:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darthdeus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/darthdeus/32/3609_2.png) [@darthdeus](https://discuss.emberjs.com/u/darthdeus)\
**Post date:** [March 24, 2013, 10:12am UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/8 "2013-03-24T10:12:04Z")

</div>

As @wycats [said here](http://discuss.emberjs.com/t/control-vs-itemcontroller/493/10) 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? 🙂

---

<div class="post-metadata">

**Author:** ![thingista](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@thingista](https://discuss.emberjs.com/u/thingista)\
**Post date:** [March 24, 2013, 1:48pm UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/9 "2013-03-24T13:48:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jun\_Andrew\_Hu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jun_andrew_hu/32/224_2.png) [@Jun\_Andrew\_Hu](https://discuss.emberjs.com/u/Jun_Andrew_Hu)\
**Post date:** [September 27, 2013, 6:05pm UTC](https://discuss.emberjs.com/t/idiomatic-way-to-handle-nested-objects/527/10 "2013-09-27T18:05:21Z")

</div>

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?
