Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You initial code looks a little tangled up. Here is a better way to achieve the same result:</p> <p><a href="http://jsfiddle.net/XaN8T/1/" rel="nofollow">http://jsfiddle.net/XaN8T/1/</a></p> <p>You should wrap each item in a controller, which is easily done with the <code>{{each}}</code> helper:</p> <pre><code>&lt;script data-template-name="application" type="text/x-handlebars"&gt; This is a list: &lt;ul&gt; {{each controller itemController="item" itemViewClass="App.ItemView"}} &lt;/ul&gt; &lt;/script&gt; </code></pre> <p>And create a separate template for your items:</p> <pre><code>&lt;script data-template-name="item" type="text/x-handlebars"&gt; &lt;h6&gt;Person:&lt;/h6&gt; &lt;div {{bindAttr class=":content active"}}&gt; &lt;dl&gt; &lt;dt&gt;First Name&lt;/dt&gt;&lt;dd&gt;{{first}}&lt;/dd&gt; &lt;dt&gt;Last Name&lt;/dt&gt;&lt;dd&gt;{{last}}&lt;/dd&gt; &lt;/dl&gt; &lt;/div&gt; &lt;/script&gt; </code></pre> <p>Then you can take advantage of <a href="http://darthdeus.github.com/blog/2013/01/27/controllers-needs-explained/" rel="nofollow"><code>needs</code></a> and computed properties in Ember with the <code>active</code> property:</p> <pre><code>App.ItemController = Ember.ObjectController.extend({ needs: ['application'], //This way each ItemController can access the ApplicationController's content (i.e. the list of all items) active: function() { return this.get('controllers.application.lastObject') === this.get('content'); }.property('controllers.application.lastObject') }); </code></pre> <p>The <code>App.ItemView</code> then binds its css class to the controller's <code>active</code> property:</p> <pre><code>App.ItemView = Ember.View.extend({ tagName: 'li', templateName: 'item', classNameBindings: ['controller.active'] }); </code></pre> <p>Voila! It works :)</p>
 

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