Search code examples
ember.jsember-dataquery-string

Formatting query parameters that are arrays in Ember.Data


Let's say that I have an Ember.Data query I'd like to make:

this.store.find('items', {itemIds: [1,2,3]});

By default, Ember.Data creates a URL that looks like this:

items?itemIds%5B%5D=1&itemIds%5B%5D=2&itemIds%5B%5D=3

But the REST api I am connecting to wants it in this format:

items?itemIds=1&itemIds=2&itemIds=3

How do I achieve this adaptation?


Solution

  • Extend the RESTAdapter and override the ajax method and create the URL that you want to use based on the circumstances.

    App.ItemsAdapter = DS.RESTAdapter.extend({
      ajax: function(url, type, options){
        if(myCircumstance){
          var data = options.data;
          delete options.data;
          url = url + ......;
        }
        return this._super(url, type, options);
      }
    });
    

    REST Adapter implementation: https://github.com/emberjs/data/blob/v1.0.0-beta.16.1/packages/ember-data/lib/adapters/rest-adapter.js