PJC
PJC

Reputation: 997

Ember JS: nested objects and architecture

I am having a lot of (painful) problems with nested objects in Ember. I think I might be tackling things the wrong way since I suspect it is a fairly standard thing to do.

Say I have an Object A that "has many" B, and each B "has many" C, etc (this architecture basically matches my relational DB schema).

I don't need nested routes for these, so I thought a natural way to render an object like A was to use partials. Something like:

Template for A:

...A stuff...
{{#each Bs}}
    {{ partial "show_B" }}
{{/each}}

and so on.

The problem is I would like the child objects to have their own controllers. I know that there is the new itemController since RC1 (http://emberjs.com/blog/2013/02/15/ember-1-0-rc.html) which is useful, but somehow limited. Let's say that further down in the hierarchy, C objects each have a single D object. So C template would look like this:

... C stuff...
{{#with c.D}}
    {{ partial "show_D" }} // How do I get this partial to have its own controller? 
{{/with}}

I guess the new {{control}} could help here, but I am having a lot of troubles using it (context not being set properly). In addition, it seems to me if it has just been added (and is still very much under development) it probably means there should be another way to do that. Lastly, it feels a bit weird to use two different APIs: "itemController" for list of items, and "{{control}}" for single items - at the end of the day, I am just trying to tie an object to a controller in both cases.

Could someone point me in the right direction here?

Thanks!

PJ

Upvotes: 0

Views: 408

Answers (1)

Teddy Zeenny
Teddy Zeenny

Reputation: 3971

I believe you are already in the right direction. You need to use control. I suppose that's why it was added.

It seems to me that the difference between itemController and control is that itemController does not require a corresponding view/template so you would still be in the same view and your template continues inline, whereas in control you need a separate view and template.

If one of control or itemController were to win, I think it would be control because it currently works in both cases (arrays and single objects) and can currently replace itemController.

Note if you had used control for the {{#each Bs}} instead of itemController, you would need to define a controller, view and template instead of a partial (or just let Ember.js generate them for you).

Like this:

{{#each Bs}}
  {{control "showB" this}}
{{/each}}

And for the D:

{{control "showD" D}}

But I agree with you, control is still buggy, and itemController is so similar to control that it doesn't make sense to have both. Both are relatively new, so I think that's why you're finding difficulties. But that's a discussion for github or discuss.ember.js.com

One ridiculous workaround would be to make a computed property that consists of an array containing D and use each dArray and itemController :) But it's obviously better to try to make control work for you.

Worst case scenario, use {{view}} helper and put some logic in the view making it act as a controller (although that's not recommended).

Upvotes: 0

Related Questions