Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Each:</strong></p> <pre><code>App.myArray = [1,2,3] {{#each value in App.myArray}} &lt;p&gt;{{value}}&lt;/p&gt; {{/each}} </code></pre> <p>corresponding HTML</p> <pre><code>&lt;p&gt;1&lt;/p&gt; &lt;p&gt;2&lt;/p&gt; &lt;p&gt;3&lt;/p&gt; </code></pre> <p><strong>Collection:</strong></p> <pre><code>{{#collection contentBinding="App.myArray"}} &lt;p&gt;{{view.content}}&lt;/p&gt; {{/collection}} </code></pre> <p>corresponding HTML</p> <pre><code>&lt;div class="ember-view"&gt; &lt;p&gt;1&lt;/p&gt; &lt;/div&gt; &lt;div class="ember-view"&gt; &lt;p&gt;2&lt;/p&gt; &lt;/div&gt; &lt;div class="ember-view"&gt; &lt;p&gt;3&lt;/p&gt; &lt;/div&gt; </code></pre> <p>As you can see both deal with arrays. In a nutshell <code>each</code> is used to display an array of elements while <code>collection</code> is used to display an array of views</p> <p>The main difference comes in practical use is when you want to interact with elements. If you just want to display a list of array use <code>each</code> helper.</p> <p>But if you want to interact with each of the element in the array while maintaining context of the clicked element you <code>collections</code></p> <p>Let me explain with an example</p> <pre><code>App.CollectionView = Ember.CollectionView.extend({ //Here the content belongs to collection view content: [1,2,3], itemViewClass: Ember.View.extend({ /*This itemViewClass is like a template which is used by all the elements in the content array of collection view*/ click: function(){ //Now this itemViewClass has a content property which is the single element in the collections content /* As you see the content of collection view has 3 elements =&gt; 3 templates 1 single template = Ember.View.extend with it's own content property first template has content set to 1 second template has content set to 2 and so on... */ alert("you clicked"+this.get('content'); } }) }) </code></pre> <p>I think this clears your doubt...</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