Note that there are some explanatory texts on larger screens.

plurals
  1. POemberjs - strange workaround for filtering based on controller property
    primarykey
    data
    text
    <p>I am trying to figure out why filtering on a controller's property directly is not working as expected.</p> <p>controller:</p> <pre><code>var VideoIndexController = Em.ArrayController.extend({ genreFilter: "", actions: { filterGenre: function(genre) { this.set("genreFilter", genre); } }, genres: function() { var genres = this.get('content').map(function(video) { return video.get("GenreName");; }).filter(function(genre, index, arr) { return arr.indexOf(genre) == index; }); console.log("genres", genres); return genres }.property("content"), filteredContent: function() { var content = this.get("arrangedContent"); if (!Ember.isEmpty(this.get("genreFilter"))) { return content.filterBy("GenreName", this.get("genreFilter").valueOf()); } else { return content; } }.property("arrangedContent", "genreFilter") }); </code></pre> <p>console.log for genres:</p> <pre><code>["Youth/Children", "Movies", "Actuality/Information", "Series", "Shows", "Sport", nextObject: function, firstObject: undefined, lastObject: undefined, contains: function, getEach: function…] 0: "Youth/Children" 1: "Movies" 2: "Actuality/Information" 3: "Series" 4: "Shows" 5: "Sport" __ember1384338760148_meta: Meta _super: undefined length: 6 __proto__: Array[0] </code></pre> <p>template:</p> <pre><code>{{outlet}} &lt;a href="" {{action "filterGenre"}}&gt;All&lt;/a&gt; {{#each genres}} &lt;a href="" {{action "filterGenre" this}}&gt;{{this}}&lt;/a&gt; {{/each}} &lt;a href="" {{action "sortAZ"}}&gt;A-Z&lt;/a&gt; &lt;a href="" {{action "sortZA"}}&gt;Z-A&lt;/a&gt; &lt;a href="" {{action "sortSuggested"}}&gt;Suggested&lt;/a&gt; &lt;section &gt; {{#each filteredContent}} {{#linkTo 'catchup/item' this}} &lt;div class="catchup-list-item"&gt; {{#if thumbnailUrl}} &lt;div&gt; &lt;img {{bindAttr src="thumbnailUrl"}} /&gt; &lt;/div&gt; {{/if}} &lt;div {{bindAttr class=":title validDownloadUrl::invalid"}}&gt; &lt;div&gt;{{ProgramName}}&lt;/div&gt; &lt;div&gt;{{subTitle}}&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; {{/linkTo}} {{/each} &lt;/section&gt; </code></pre> <p>notice the 2nd parameter in <code>content.filter()</code></p> <p>if I call this like:</p> <p><code>content.filterBy("GenreName", this.get("genreFilter"));</code></p> <p>then the returned array is always empty.</p> <p>adding <code>.valueOf()</code> gives me the expected output. This seems pretty cumbersome and error prone.</p> <p>edit: note that I am using <code>arrangedContent</code>, as I have some snipped actions that perform sorting</p> <p>edit2: updated to include template</p> <p>edit3: added genres array console log</p>
    singulars
    1. This table or related slice is empty.
    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. 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