Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My original answer is at the bottom, but the latest Ember releases provide a really nice new solution for this:</p> <pre><code>genres: Ember.computed.mapBy('songs', 'genre'), uniqueGenres: Ember.computed.uniq('genres'), </code></pre> <p>References: <a href="http://emberjs.com/api/classes/Ember.computed.html#method_mapBy">mapBy</a> and <a href="http://emberjs.com/api/classes/Ember.computed.html#method_uniq">uniq</a>. Computed property macros are great =)</p> <p>previous answer:</p> <pre><code>genres: function() { return this.get('songs').map(function(song){ return song.genre }).filter(function(genre, index, arr){ return arr.indexOf(genre) == index // throw away any instances which are not first }) }.property('songs'), </code></pre> <p>note, I'm leveraging 'modern' array functions such as map and filter here, if you need to support older browsers, you'll need to shim those or write this another way. The filter I used for removing duplicates is the first that occurred to me, it's probably not very performant and if you've already loaded a library with a unique function (like <a href="http://underscorejs.org/#uniq">underscore.uniq</a>), you should use that.</p> <p>Very little of this is Ember specific of course, the <code>.property('songs')</code> on the end is literally all you need to specify that this is a computed property, which will be cached and recomputed if songs changes (assumes Ember 1.0)</p> <p>References: MDN for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">map</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">filter</a></p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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