question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using offset parameter in backend request

See original GitHub issue

The app I am working on has two variables for pagination: limit and offset. Offset indicates just the starting point from which row the selection in the database table should start, the limit parameter tells how much rows should be selected.

Of course, limit is equal to the _perPage and can easily be customized by using the perPageParam. But offset is a multiplication of the currentPage and _perPage variables.

By using the bound parameters, the request send to the backend can be created:

import InfinityRoute from "ember-infinity/mixins/route";

export default Ember.Route.extend(InfinityRoute, {
  perPageParam: 'limit',

  offset: Ember.computed('currentPage', '_perPage', function() {
    return this.get('currentPage') * this.get('_perPage');
  }),

  model() {
    return this.infinityModel('model-name', {}, { offset: 'offset' });
  }
}

As this works nice, I am using the private variable _perPage. Is there a way to create this request avoiding the use of a private variable?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
queenvictoriacommented, Jan 15, 2019

Now that the route mixin is deprecated you can do this by extending InfinityModel.

// routes/myroute.js
import Ember from 'ember';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { computed, get, set, getProperties } from '@ember/object';

import { typeOf } from '@ember/utils';
import RSVP from 'rsvp';
import InfinityModel from 'ember-infinity/lib/infinity-model';
import { objectAssign } from 'ember-infinity/utils';


// Extend InfinityModel to support offset rather than page number
const ExtendedInfinityModel = InfinityModel.extend({
  buildParams(increment) {
    const pageParams = this._super(...arguments);

    let { pageParam } = getProperties(this, 'pageParam');
    if (typeOf(pageParam) === 'string' && pageParam.indexOf('offset') > -1 ) {
      pageParams[pageParam] = (get(this, 'currentPage') + increment) * get(this, 'perPage');
    }

    return objectAssign(pageParams, get(this, 'extraParams'));
  },
});


export default Route.extend({
  infinity: service(),

  model() {
    // Return multiple models.
    return RSVP.hash({
      // Our resources for this route.
      items: this.get("infinity").model('mymodel', {
        // Infinity parameters.
        perPage: 16,
        startingPage: 0,

        // Configuring Infinity.
        perPageParam: 'page[limit]',
        pageParam: 'page[offset]',
        countParam: 'meta.page.total',

        // OPTIONAL.
        // Our additional parameters.
        include: 'relatedmodel,relatedmodel.nestedmodel',
        sort: 'name'
      }, ExtendedInfinityModel.extend()),

      othermodel: this.get("store").findRecord("othermodel", "othermodel_id"),
    });
  },
});
{{!-- templates/myroute.hbs --}}
{{#each model.items as |item|}}
{{model.name}}
{{/each}}

{{infinity-loader infinityModel=model.items }}
0reactions
hhffcommented, May 24, 2016

@jevanlingen - _perPage is set by the initial model invocation, and is private for that reason. Ember Infinity assumes that perPage is not dynamic - by making it public we’re likely to run into some wacky problems for some users. I’d rather leave it private and let the savvy use it when they know what they’re doing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

offset API parameter - Algolia
Offset is the position in the dataset of a particular record. By specifying offset , you retrieve a subset of records starting with...
Read more >
Paging Through Results Using Offset and Limit
To get a different set of items, you can use the offset and limit parameters in the query string of the GET request....
Read more >
Offset-based Pagination - Box Developer Documentation
Offset -based pagination is often used where the list of items is of a fixed and predetermined length. Paging. To fetch the first...
Read more >
Offset Parameter - REST API Reference
The offset query parameter is used to exclude from a response the first N items of a resource collection. For example, to show...
Read more >
How can I use Python requests to paginate api calls with offset ...
The offset parameter controls the starting point within the collection of resource results. If you have a collection of 15 items to be ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found