0

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

4

1 回答 1

0

我相信你已经在正确的方向。你需要使用control. 我想这就是它被添加的原因。

在我看来,和之间的区别在于itemController不需要control相应itemController的视图/模板,因此您仍将处于同一视图中,并且您的模板继续内联,而control您需要单独的视图和模板。

如果其中一个control获胜itemController,我认为这是control因为它目前在两种情况下(数组和单个对象)都有效,并且当前可以替换itemController.

请注意,如果您使用control{{#each Bs}}而不是itemController,则需要定义控制器、视图和模板而不是 a partial(或者让 Ember.js 为您生成它们)。

像这样:

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

对于 D:

{{control "showD" D}}

但我同意你的观点,control仍然是错误的,并且itemController非常相似control,两者兼有是没有意义的。两者都相对较新,所以我认为这就是您遇到困难的原因。但这是对 github 或讨论的讨论。ember.js.com

一个荒谬的解决方法是创建一个由包含 D 和 use 的数组组成的计算属性,each dArray并且itemController:) 但尝试control为您工作显然更好。

在最坏的情况下,使用{{view}}帮助器并在视图中放置一些逻辑,使其充当控制器(尽管不推荐这样做)。

于 2013-03-16T16:19:07.417 回答