Ember.js {{each}} vs {{collection}} [英] Ember.js {{each}} vs {{collection}}

查看:79
本文介绍了Ember.js {{each}} vs {{collection}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白每个集合帮助方法是两种可能的方式迭代我的句柄中的项目列表模板。我正在寻找关于何时使用每个 collection 的实用建议,两种方法之间有什么区别。

解决方案

每个

  App.myArray = [1,2,3] 

{{#each value in App.myArray}}
< p> {{value} }< / p为H.
{{/ each}}

对应的HTML

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

集合:

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

对应的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>

正如您可以看到的那样处理数组。简而言之,每个用于显示元素数组,而 collection 用于显示视图数组

主要区别在于实际使用的是当您想要与元素交互时。如果你只想显示一个数组列表,使用每个帮助器。



但是,如果你想与每个的元素,同时保持点击元素的上下文集合



让我用一个例子来解释

  App.CollectionView = Ember.CollectionView.extend({
//这里的内容属于集合视图
内容:[1,2,3],
itemViewClass:Ember.View.extend({
/ *)这个itemViewClass就像一个由
使用的模板,内容中的所有元素数组的集合视图* /
点击:function(){
//现在这个itemViewClass有一个content属性,它是集合内容中的单个元素
/ *正如你看到的内容集合视图有3个元素=> 3个模板
1个单一模板= Ember.View.extend与它自己的内容属性
第一个模板的内容设置为1
s econd模板的内容设置为2,依此类推...
* /
alert(你点击+ this.get('content');
}
})
})

你的怀疑...


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 practical advice about when to use each or collection and what are the differences between the two methods.

解决方案

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

这篇关于Ember.js {{each}} vs {{collection}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆