Get record id without making a request


#1

Hi Guys,

I’ve been using ember + ember data from more than 5 months ago and I have a problem when I’m trying to render the id of a record in a simple list, without making a request to my Api for each record

Here is my code:

//Models
App.Team = DS.Model.extend({
  name: DS.attr('string'),
  managerNickname: DS.attr('string'),
  user: DS.belongsTo('user')
});

 App.User = DS.Model.extend({
  email: DS.attr('string'),
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
 });
 
 //Template
 {{#each team in this}}
    {{team.user.id}}
 {{/each}}

The line {{team.user.id}} makes a request!

So, my question is: Why this making a request if I only I’m acceding to the user id? this is a bug or is the expected behavior? I’ve been thinking about it a lot and I have not found sense for this, I mean, if I currently have the user id why I need to do these request??

Thanks for reading and I hope your answers!


#2

I’ve been struggling with this as well. It’s specially noticeable when you are linking models.

{{#each team in this}}
  {{link-to "user" team.user}}
{{/each}}

I believe the PromiseProxy that DS.belongsTo('user') is returning should not delegate the id and use the one coming from the backend instead, so it doesn’t even have the need of getting a model back unless you ask for something other than the id.

Does that make any sense? Is it worth a PR to ember-data?


#3

@josepjaume that would be really useful!


#4

I think this is worth a PR!


#5

T.O.T.A.L.L.Y. agree. Specially for links to other resources. I don’t show any detail of the object in the link, so I don’t want to fetch the data until I enter its route!

In fact I think that this should be used everywhere. I think that the object we have in ember-data relations should not fetch data unless we ask for an attribute that has already been set.