getoutreach / epf

A framework for keeping your Ember.js apps in sync.
http://epf.io
MIT License
369 stars 33 forks source link

Ep.LazyModel breaks {{#linkTo}} in deep relationships #60

Closed hperl closed 2 years ago

hperl commented 11 years ago

The linkTo helper breaks when I want to link to a model in a HABTM relationship with an explicit join model. It looks like since the join model is lazy loaded, the property for which the linkTo helper needs to create the route does not even exist.

Consider the following example:

App.Article = Ep.Model.extend({
  title: Ep.attr('string'),
  authorships: Ep.hasMany(App.Authorship)
})
App.Authorship = Ep.Model.extend({
  authorships: Ep.hasMany(App.Authorship),
  person: Ep.hasMany(App.Person)
})
App.Person = Ep.Model.extend({
  name: Ep.attr('string'),
  authorships: Ep.hasMany(App.Authorship)
})

articles/show.hbs

{{#each authorships}}
  {{! This does not work:}}
  {{#linkTo "people.show" person}}{{person.name}}{{/linkTo}}
  {{! This works, but looks unnecessarily ugly}}
  {{#if person.isLoaded}}
    {{#linkTo "people.show" person}}{{person.name}}{{/linkTo}}
  {{/if}}
{{/each}}

I get the following errors:

Assertion failed: Cannot call get with 'id' on an undefined object. 
Uncaught TypeError: Cannot read property '__ember1375951619363_meta' of undefined

It seems like that since in the article's view, the authorship is still a lazy model, a lookup of the person property of the lazy model fails and the route generation fails as well.

Any ideas?

This is possibly related to issue #34.

ghempton commented 11 years ago

I'm going to mark this as an enhancement. The issue is that lazy models do not make their associations available until they are loaded. One possible fix would be to make lazy models return lazy models for their associations as well.

belluzj commented 11 years ago

This would be really great, especially if the lazy associations were to fill themselves with content later. It would be very useful to me because most of my routes use relationships as their models (to get auto-updating).

It works well when navigating from to root route to sub routes, but is does not when getting directly to a sub route with an url. (because the relationships along the way are not available, I think).

I also had problems when trying to observe properties of one model from another through relationships: EPF complained that something should be attached to a session. I suppose it comes from LazyModels, because I saw that a lot in the debugger :) but I can't be sure.

jimsynz commented 10 years ago

is there a workaround for this issue?

jimsynz commented 10 years ago

FYI, this can also happen if you forget to pass the model to the link-to :)