Emberjs-data限制集合

时间:2013-08-06 10:29:58

标签: javascript ember.js ember-data

我有这个jsbin,是对todos示例的一个非常小的修改

http://jsbin.com/ubimes/1/

我正试图找到将模型限制为最后5个条目的方法

 return Todos.Todo.find({limit:5});

但该查询不返回任何内容

知道如何限制收藏吗?

2 个答案:

答案 0 :(得分:3)

您可以使用切片而不是拼接

  

this.get('content')。slice(0,2);

答案 1 :(得分:1)

您可以这样做的方法可能是限制您的收藏品在您控制数据的控制器中的内容,如下所示:

App.IndexController = Ember.ArrayController.extend({
  limitedContent: function() {
    // in this case '2' is the limit parameter
    return this.get('content').splice(0, 2);
  }.property('content')
});

.property('content')定义了对控制器内容的绑定,并注意#each帮助器在content更改时会重新呈现。

然后在您的模板中,您会遍历limitedContent,而不是content

<script type="text/x-handlebars" data-template-name="index">
  {{#each color in limitedContent}} 
    {{color.name}}
  {{/each}}
</script>

这里有一个显示上述概念的工作jsbin

希望有所帮助

相关问题