0
votes

This is my second Ember Application, and the following problem has been a reoccurring theme.

I am using the Canary build of both Ember js and Ember data with query params enabled.

Say we have the following relationships:

  • A has many B
  • A has many C
  • A has many D
  • A has many E

Model B, C, D, E are huge models and resource heavy for rendering. So loading them ALL the time is not optimal.

Say if my application has a dashboard that allows for a quick glance of the state of the application, and it focuses on model A. I don't need to display anything from B, C, D, E. So the JSON will not return the relationship ID array from the example.com/api/a endpoint.

And if the user clicks to the show route of model A then How should I load models B, C, D, E?

Thanks All

2

2 Answers

1
votes

Bill, I answered your question on the Ember discussion forum, but I'm going to copy the answer here as well, just so the question is answered.

I've been using a very old version of Ember Data for a long time, and I just updated to the latest version a few days ago. Because of this, take what I say with a grain of salt. :) But I believe that Ember Data only loads records/relationships lazily. So you should return the IDs for B, C, D, E along with the JSON for A, and then Ember Data should load the records for them automatically as soon as you reference them.

EDIT: Almost forgot. You must include the async: true option for this to work.

App.A = DS.Model.extend({
    b: DS.hasMany('b', { async: true });
});
0
votes

This is probably more trouble than its worth... Ember convention would be to load A with it's relationship IDs to B,C,D,E. If you're caching your responses on the server, this should be faster the second time around.

Otherwise, you might be able to change the ajaxPrefilter before your request on the dashboard page to have some header that you can use in your server to change its default response.

Ember.$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
 jqXHR.setRequestHeader('no_relationship_ids', 'true');
});

Then in your show page, you'll always have to .reload() the model before rendering (and obviously not include the above header).