0
votes

I have route :

  model: function(params) {
    return this.store.findRecord('singleUser', params.user_id, {reload: true});
  },

  setupController: function(controller, model) {
    this._super(controller, model);
    this.store.find('userNetwork', {userId: model.id});
  },

And two models:

models/single-user.js

userNetworks: DS.hasMany('userNetwork', {async: true})

models/user-network.js

singleUser: DS.belongsTo('singleUser'),

after that my model makes request to server:

GET "server/api/userNetworks?userId=270".

response from the server:

{"singleUsers":{"userNetworks":[40]}}

it's right, but makes another one request:

GET "server/app_dev.php/api/userNetworks/40".

Help me. Why is this happening ?

Ember v1.13.7 Ember-data v1.13.9

1
Because you're making 2 store.finds ?kris
I would put the second find in an afterModel, rather than setupController. That way you're sure that it's done loading before the transition is finalized and the template is rendered.user663031

1 Answers

0
votes

Are you assuming that this.store.find('userNetwork', {userId: model.id}); should return the userNetwork from your model without an additional request because you've already gotten the model?

You're using async: true on the single-user hasMany relationship with userNetworks. This means you're specifying that you will request the related userNetworks separately, which means it will make a separate request.

Also, from the code you posted it looks like you most likely wanted to do:

setupController: function(controller, model) {
  this._super(controller, model);
  model.get("userNetworks");
},

Which will still make an additional request but is the correct way to get the related records.

If you don't want it to make additional requests, you could use the EmbeddedRecordsMixin and return the related userNetworks with each user model.

More on EmbeddedRecordsMixin here

P.S. @torazaburo is right on using afterModel vs. setupController.