Search code examples
ember.jsember-datajson-api

preventing a duplicate request in Ember Data when trying to set the route model to the result of an association


I have a route where I need to fetch associated data that is not available in the parent routes, so I need to basically reload the model and in the process provide JSONAPI with the include directive to embed other models. So the route looks like this.

import Ember from 'ember';

// route for patients/1/appointments
export default Ember.Route.extend({
  model: function() {
    const query = {
            id: this.modelFor('patient').get('id'),
            include: 'appointments,appointments.practitioner'
          },
          success = function(patient) {
            return patient.get('appointments');
          };

    return this.store.queryRecord('patient', query).then(success);
  }
});

The success callback is fetching the appointments a second time, which maybe isn't surprising, but it should seemingly also know that it has those in the store locally. So, I'm trying to resolve a reasonable way to set the model to the set of appointment models coming back. For various reasons, we don't want the logic of this specific request to live in the adapters, since (for example) we may not always need the practitioner side loaded anytime we get the patient's appointments. Any ideas?


Solution

  • I think that there must be some issue with your response from server after first queryRecord.

    I created simple Ember Twiddle that matches your example and is based on mockjax here. As you can see when you open Console, the mockjax is getting only two requests - first for /patient/1 and second for /patients?... (your queryRecord).

    Even though I am rendering all appointments that are relationship to current patient, mockjax does not get any other query for relationships. You can check out the JSON responses I provided for mockjax. You should compare them with your API to see if there are different in structure.