如何限制在路由中使用this.store.find返回的记录数

时间:2014-11-23 19:51:56

标签: ember.js

我的索引路线中有以下代码。如何限制返回的记录数?我在路由的setupController中设置了this._super(controller,model)。

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return Ember.RSVP.hash({
      articles: this.store.find('article'),
      categories: this.store.find('category', {limit: 3})
    });
  },
  setupController: function(controller, model) {
    // No records are retrieved without this
    this._super(controller, model);
  }
});

1 个答案:

答案 0 :(得分:3)

没关系。我重新阅读了一个Ember.js教程,并意识到limit只是我的API的一个参数;所以我将它设置在我的Rails API控制器中并使其正常工作。

  def index
    if params[:limit].present?
      @articles = Article.limit(params[:limit])
    else
      @articles = Article.all
    end
    render json: @articles
  end

您还可以使用Ember.js中的slice进一步限制存储的记录:

categories: this.store.find('category', {limit: 10}).then(function(result) {
  return result.slice(0,3);
})

非常感谢#ember.js IRC聊天室中的@locks。