Search code examples
ember.jsember-datajson-api

Ember data side loading with JSONAPI


The request generated for my route is http://api.myApp.com/tags/123/products, but I need to do some side loading to improve performance, the desired XHR would be:

http://api.myApp.com/tags/123/products?include=sideload1,sideload2,sideload3

My router looks like this:

  this.route('tags', function() {
    this.route('tag', { path: ':id' }, function() {
      this.route('products', function() {

      });
    });
  });

I'll like to sideload some async models for products, currently I have:

// app/routes/tags/tag/products.js

model() {
    return this.modelFor('tags.tag').get('products');
}

How would I go about adding query params in route?


Solution

  • I'm doing something similar in a project and store.query(type, { query }); has worked for me. http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_query-and-queryrecord

    Try doing a store.query when defining your model and passing in

    {include: "sideload1,sideload2,sideload3"}
    

    Another option could be creating an adapter for your model and using buildURL to add on the query params... However this is a bit hacky and shouldn't be necessary if your API is following the JSON API standards.

    App.TagsAdapter = DS.JSONAPIAdapter.extend({
        buildURL: function(type, id) {
            var baseURL = 'http://api.myApp.com/tags/123/products';
            var method = '?include=sideload1,sideload2,sideload3';
    
            return baseURL + method;
        }
    });