[SOLVED] JSONAPIAdapter making extra GET

I’m bringing up a rails-api/AMS back-end and an ember front-end, connected via jsonapi adapters.

I’ve set up support for “include” directives on a page that references a client model, and it’s distributor parent (belongsTo relationship). I see Ember make the request to the jsonapi endpoint with the include directive, I see the back-end response come back with the distributor included, but then Ember immediately goes and does another GET to (re) fetch the distributor. I’m kind-of guessing this is promise-fulfillment going awry, but I don’t know. Here’s what I’ve got:

Client model:

import DS from ‘ember-data’; import Organization from ‘./organization’;

export default Organization.extend({ name: DS.attr(‘string’), address1: DS.attr(‘string’), distributor: DS.belongsTo(‘distributor’, { async: true }) });

Distributor model:

import DS from ‘ember-data’; import Organization from ‘./organization’;

export default Organization.extend({ name: DS.attr(‘string’), address1: DS.attr(‘string’), clients: DS.hasMany(‘client’, { async: true } ) });

Client show route:

export default Ember.Route.extend({ model(params) { return this.store.findRecord(‘client’, params.client_id, { include: ‘distributor’ } ); }, });

Client show template references to distributor:

{{model.distributor.id}} - {{model.distributor.name}}

First HTTP request:

http://myserver.local:3000/api/v1/clients/2?include=distributor GET

Response:

{“data”:{“id”:“2”,“type”:“clients”,“attributes”:{“name”:“B Client”,“address1”:“112 Other Place”},“relationships”:{“distributor”:{“data”:{“id”:“2”,“type”:“distributors”}},“users”:{“data”:}}},“included”:[{“id”:“2”,“type”:“distributors”,“attributes”:{“name”:“Another Distributor”,“address1”:“102 Some Place”},“relationships”:{“clients”:{“data”:[{“id”:“2”,“type”:“clients”}]},“users”:{“data”:}}}]}

And that request is immediately followed by another request:

http://myserver.local:3000/api/v1/distributors/2

So- the point of the include directive is to allow me to specify additional data to be loaded with a single request. Any ideas why I’m getting the second request?

OK, so this turned out to be my naivety with nested routes. /clients/1 was nested under /clients, which fetches all clients and their distributors. Reloading /clients/1 ember route triggered the route above it to fetch models, and that was the origin of my confusion.

I moved this route to its own top-level to verify, and sure enough, the sideloading worked as expected, fetching the client and distributor with a single request.

Lesson learned: always pay attention to parent route fetches.