Render Multiple View/Controller Pairs

2019-07-22 11:39发布

问题:

I am currently rendering a list of views:

  <ul>
    {{#each newsItem in controller}}
      {{view App.NewsItemView contentBinding="newsItem" class="news-item" }}
    {{/each}}
  </ul>

But I would like to inject a NewsItemController into each view.

I've tried using render, but this only seems to support a single view, giving the exception:

Uncaught Error: Handlebars error: Could not find property 'control' on object .

I've found a brief mention of using control instead, but this no longer seems to be included.

So how can I render multiple versions of the same view, injecting a separate controller into each one?

回答1:

{{render}} should be fixed in current master (if you build it from Github). You should be able to use it multiple times if you pass a model:

<ul>
 {{#each controller}}
   {{render "newsItem" this}}
 {{/each}}
</ul>

{{control}} is still there but hidden behind a flag (because it's still experimental). To use it you need to do : ENV.EXPERIMENTAL_CONTROL_HELPER = true before including the ember.js file. If you can avoid using it, it would be better.

However I think the simplest approach would be to use itemController:

<ul>
  {{#each controller itemController="newsItem"}}
    {{view App.NewsItemView class="news-item" }}
  {{/each}}
</ul>

I think you can combine them to make it simpler (I haven't tried it yet):

<ul>
  {{each controller itemController="newsItem" itemViewClass="App.NewsItemView"}}
</ul>