0
votes

I have the following code in my Index route. How do I limit the number of records returned? I have this._super(controller, model) set in the route's setupController.

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

1 Answers

3
votes

That's ok. I re-read an Ember.js tutorial and realised limit is just a parameter for my API; so I set it in my Rails API controller and got it working.

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

You can also further limit the stored records using slice in Ember.js:

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

Big thanks to @locks on the #ember.js IRC chatroom.