Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Links Relationships not lazy loaded? #1254

Closed
JaredSartin opened this Issue · 13 comments

4 participants

Jared Sartin Yehuda Katz kdemanawa Chris Atkins
Jared Sartin

I have an article model in Ember/EmberData that hasMany related article, which in turn are article models.

App.Article = DS.Model.extend
  title: DS.attr 'string'
  url: DS.attr 'string'
  user: DS.belongsTo('user')
  collection: DS.belongsTo('collection')
  related_articles: DS.hasMany('article', async: true)

My server was serializing out the related articles as an array of IDs, but we wanted to move all hasMany relationships to use the links attribute and build an endpoint to pull the data from. When the related articles come out as IDs they are only loaded to the first level, which is all we need. When the related articles come through as the links association with the URL endpoint, EmberData decides to find all related articles for each child, their children, and so on until all articles in the relational tree are loaded.

We are displaying one article, with its associated related articles. At the point of display, we don't care about any related articles of the children, so no need to load them now. Is this intentional, skipping lazy load, or is this a bug?

Yehuda Katz
Owner

It should be relatedArticles.

What exactly is triggering the fetch?

Jared Sartin

I am unsure where the fetch is being triggered... Is there some magical logging hidden somewhere that can help? ;)

I will give it a look this morning and see if I can pinpoint it.

Jared Sartin

Also, what needs to be relatedArticles? the attribute or the hasMany?

Yehuda Katz
Owner
App.Article = DS.Model.extend
  title: DS.attr 'string'
  url: DS.attr 'string'
  user: DS.belongsTo('user')
  collection: DS.belongsTo('collection')
  relatedArticles: DS.hasMany('article', async: true)
kdemanawa

Hi @wycats it would be nice if we can have the ability to customize the key names of the links. Right now, it is directly using the relationship name to get the url from the links.

Yehuda Katz
Owner

@kdemanawa the idea is that you can implement normalize and rename anything you need to rename.

Yehuda Katz
Owner

@JaredSartin I can't identify what might be causing this by reviewing the Ember Data code, so I'm closing this. However, please comment here with more information on what code path is triggering the fetch if you can identify it.

Yehuda Katz wycats closed this
Jared Sartin
kdemanawa

@wycats thanks!!

Jared Sartin

I have ember and ember data updated and am still running into this problem... I will try to get some information about it. Unfortunately this doesn't help:

App = Ember.Application.create({
    LOG_TRANSITIONS: true,
    LOG_TRANSITIONS_INTERNAL: true,
    LOG_BINDINGS: true,
    LOG_ACTIVE_GENERATION: true,
    LOG_VIEW_LOOKUPS: true,
});
Chris Atkins

@wycats I'm running into the exact same problem.

App.Score = DS.Model.extend
  percentage: DS.attr 'number'
  owner: DS.belongsTo 'scoreOwner', polymorphic: true
  subject: DS.belongsTo 'scoreSubject', polymorphic: true
  history: DS.hasMany 'score', async: true
  subjectType: DS.attr 'string'
  calculatedAt: DS.attr 'date'

  formattedPercentage: (->
    Math.round(@get('percentage')) + '%'
  ).property('percentage').cacheable()

I'm running recent versions of Ember Data

DEBUG: Ember      : 1.5.1+pre.ff8c6bbb ember.js?body=1:3524
DEBUG: Ember Data : 1.0.0-beta.7+canary.d5562867 ember.js?body=1:3524
DEBUG: Handlebars : 1.1.1 ember.js?body=1:3524
DEBUG: jQuery     : 2.1.0 ember.js?body=1:3524

Also using the ActiveModelSerializer with pretty minimal modifications. I'll keep digging through the call stack to try and figure out why it keeps going.

Chris Atkins

I'm not sure if this helps, but I put a breakpoint in the findHasMany method on the DS.RESTSerializer, and hit console.trace() for each one. I'm not sure if this helps, but the subsequent calls had much bigger stack traces.

Here's a gist of the output.

Chris Atkins

@JaredSartin Did you end up finding a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.