DS.RESTAdapter#sortQueryParams()

sortQueryParams (obj) Object

Defined in addon/adapters/rest.js:244

By default, the RESTAdapter will send the query params sorted alphabetically to the server.

For example:

  store.query('posts', { sort: 'price', category: 'pets' });

will generate a requests like this /posts?category=pets&sort=price, even if the parameters were specified in a different order.

That way the generated URL will be deterministic and that simplifies caching mechanisms in the backend.

Setting sortQueryParams to a falsey value will respect the original order.

In case you want to sort the query parameters with a different criteria, set sortQueryParams to your custom sort function.

app/adapters/application.js
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  sortQueryParams: function(params) {
    var sortedKeys = Object.keys(params).sort().reverse();
    var len = sortedKeys.length, newParams = {};

    for (var i = 0; i < len; i++) {
      newParams[sortedKeys[i]] = params[sortedKeys[i]];
    }
    return newParams;
  }
});

Parameters:

obj Object

Returns:

Object
doc_EmberJs
2016-11-30 16:50:33
Comments
Leave a Comment

Please login to continue.