【问题标题】:How to limit number of records returned using this.store.find in a route如何限制在路由中使用 this.store.find 返回的记录数
【发布时间】:2014-11-23 19:51:56
【问题描述】:

我的索引路由中有以下代码。如何限制返回的记录数?我在路由的 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);
  }
});

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    没关系。我重新阅读了 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 2010-11-05
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 2011-07-03
      • 2015-12-10
      • 2010-09-05
      相关资源
      最近更新 更多