简体   繁体   English

在Ember.js中显示数组中的3个项目

[英]Display 3 items from array in Ember.js

I have a source of data for Ember.js that has several items. 我有Ember.js的数据源,其中有几个项目。

I can do: 我可以:

{{each controller}}

{{name}}

{{/each}}

To display all names from this source and it works. 显示此来源中的所有名称,并且可以使用。

Now i would like to display only 3 of them, but since the template for each item is slightly different i would like to be able to do something like this: 现在我只想显示其中的3个,但是由于每个项目的模板都略有不同,所以我希望能够执行以下操作:

<h1>{{name of first item}}</h1>
<h2>{{name of second item}} </h2>
<h3>{{name of third item}}</h2>

How to acomplish it with Ember.js? 如何用Ember.js完成它?

If it's only three items that you want to pull out, you can create a computed property for each of them: 如果只想拉出三个项目,则可以为每个项目创建一个计算属性:

App.IndexController = Ember.ArrayController.extend({
  firstItem: function() {
    return this.get('model').objectAt(0);
  }.property('model'),

  secondItem: function() {
    return this.get('model').objectAt(1);
  }.property('model'),

  thirdItem: function() {
    return this.get('model').objectAt(2);
  }.property('model')
});

Here's a full example: 这是一个完整的示例:

http://emberjs.jsbin.com/fibaha/1/edit?html,js,output http://emberjs.jsbin.com/fibaha/1/edit?html,js,输出

http://emberjs.com/api/classes/Ember.Array.html#method_objectAt http://emberjs.com/api/classes/Ember.Array.html#method_objectAt

I would try to do something more along these lines as a basis. 我将尝试根据这些思路做更多事情作为基础。

Ember.ArrayController.extend({
  limitedContent: function(){
    return this.get('content').slice(0, 3);
  }.property('content.[]'),
  sortProperties: ['color'],
  sortAscending: false,
})

Then what you can do is iterate over the limitedContent. 然后,您可以做的是遍历limitedContent。 You can make this extensible by setting a dynamic limit, and if you wanted to change the way it's sorted, it will still be effective. 您可以通过设置动态限制来使其可扩展,并且如果您想更改其排序方式,它将仍然有效。 I handle pagination in a very similar way. 我以非常相似的方式处理分页。 If you wanted to handle sorting properly, you would change to getting the content to arrangedContent. 如果您想正确处理排序,则可以更改为将内容添加到describeedContent。

Here's a jsbin showing this: JSBIN 这是显示此内容的jsbin: JSBIN

You can do it with ember-composable-helpers: object-at helper 您可以使用ember-composable-helpers来做到这一点:object-at helper

{{object-at index array}} {{对象索引数组}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM