Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect view template by model type/object value using Ember.js
    primarykey
    data
    text
    <p>I would like to store different objects in the same controller content array and render each one using an appropriate view template, but ideally the same view.</p> <p>I am outputting list objects using the code below. They are currently identical, but I would like to be able to use different ones.</p> <pre><code>&lt;script type="text/x-handlebars"&gt; {{#each App.simpleRowController}} {{view App.SimpleRowView class="simple-row" contentBinding="this"}} {{/each}} &lt;/script&gt; </code></pre> <p>A cut-down version of the view is below. The other functions I haven't included could be used an any of the objects, regardless of model. So I would ideally have one view (although I have read some articles about mixins that could help if not).</p> <pre><code>&lt;script&gt; App.SimpleRowView = Em.View.extend({ templateName: 'simple-row-preview', }); &lt;/script&gt; </code></pre> <p>My first few tests into allowing different object types ended up with loads of conditions within 'simple-row-preview' - it looked terrible!</p> <p>Is there any way of dynamically controlling the templateName or view used while iterating over my content array?</p> <p><strong>UPDATE</strong></p> <p>Thanks very much to the two respondents. The final code in use on the view is below. Some of my models are similar, and I liked the idea of being able to switch between template (or a sort of 'state') in my application.</p> <pre><code>&lt;script&gt; App.SimpleRowView = Em.View.extend({ templateName: function() { return Em.getPath(this, 'content.template'); }.property('content.template').cacheable(), _templateChanged: function() { this.rerender(); }.observes('templateName'), // etc. }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload