Ember.js {{each}} vs {{collection}}

后端 未结 3 715
孤城傲影
孤城傲影 2021-02-19 17:11

I understand each and collection helper methods are two possible ways to iterate over a list of items in my handlebars templates. I am looking for some

相关标签:
3条回答
  • 2021-02-19 17:26

    I'm new to Ember.js, and I haven't used {{collection}} yet, but with what knowledge I have and a glance at the docs for {{collection}} (http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_collection), I surmise the following:

    The {{each}} helper will iterate over a list of objects and output the contents between the {{each}} tags rendered against each object. It's just a loop within a template.

    The {{collection}} helper will also iterate over a list of objects, but in each iteration it will create a new View object to contain it. If you use the block form ({{#collection}}{{/collection}}), the contents between the tags will become the template associated with the the newly created view. If you use the single-tag form ({{collection}}), instead of supplying the template right there, you specify the name of the View to use, and Ember will create views of that class (rather than generic Ember.View) and use it's associated template.

    The reason to use {{collection}} instead of {{each}} is more complex and subtle, and it seems like the kind of thing you only start to really get from encountering them while working on a real app - at least, that's been my experience so far with many parts of Ember. For example, you'll suddenly realize you need your looped template section to be a distinct view object for some reason - maybe to have somewhere to include additional event handlers, or store additional UI state specific to each loop iteration, like an isEditing flag.

    0 讨论(0)
  • 2021-02-19 17:39

    Each:

    App.myArray = [1,2,3]
    
    {{#each value in App.myArray}}
      <p>{{value}}</p>
    {{/each}}
    

    corresponding HTML

    <p>1</p>
    <p>2</p>
    <p>3</p>
    

    Collection:

    {{#collection contentBinding="App.myArray"}}
      <p>{{view.content}}</p>
    {{/collection}}
    

    corresponding HTML

    <div class="ember-view">
      <p>1</p>
    </div>
    <div class="ember-view">
      <p>2</p>
    </div>
    <div class="ember-view">
      <p>3</p>
    </div>
    

    As you can see both deal with arrays. In a nutshell each is used to display an array of elements while collection is used to display an array of views

    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 each helper.

    But if you want to interact with each of the element in the array while maintaining context of the clicked element you collections

    Let me explain with an example

    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 => 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');
        }
      })
    })
    

    I think this clears your doubt...

    0 讨论(0)
  • 2021-02-19 17:40

    As @Abdull stated in the comments to your question, the {{collection}} view helper has been deprecated and, as a result, {{each}} is the suggested usage.

    /**
      `{{collection}}` is a `Ember.Handlebars` helper for adding instances of
      `Ember.CollectionView` to a template. See `Ember.CollectionView` for
      additional information on how a `CollectionView` functions.
    
      ...
    
      @method collection
      @for Ember.Handlebars.helpers
      @param {String} path
      @param {Hash} options
      @return {String} HTML string
      @deprecated Use `{{each}}` helper instead.
    */
    

    Source code: ember.js/packages/ember-handlebars/lib/helpers/collection.js

    0 讨论(0)
提交回复
热议问题