Loading async related models when using `link-to` helper


#1

Hi. I’ve also asked this question here: http://stackoverflow.com/q/28294169/3700770

When you load a route via {{#link-to 'node' nodeModel}} in a template, the model hook does not get called in the route object. From what I’ve read in the docs, you can fetch the related models in the route’s afterModel hook in this case.

// routes/node.js
import Ember from 'ember';

export default Ember.Route.extend({
  afterModel: function(model) {
    return this.store.find('relatedModel');
  }
});

I’ve tried a number of variations on this, and nothing has worked. It seems afterModel needs to return the model that gets set on the controller’s model property, not just any promise (that’s not what I expected from reading the docs). So… what do I have to do? I haven’t found any way to load related models at all when clicking on the link. (If you refresh the page on that URL, then all the models are loaded). Thank you for any help.


#2

This works:


#3

Thank you for the example. So here we are getting a list of all authors in the posts route, and then setting the allAuthors property on the posts controller.

The author list is needed by the post editing template, which accesses the list via the post controller’s authorList function, which returns an array of author name strings.

This is interesting. The convention I would expect is to define the post.author property as a belongsTo relationship on the post model. Then that relationship would be resolved by Ember Data, so you could access the author model by calling post.get('author') or {{ model.author }} in a template.

If we had defined post.author as an async belongsTo relationship, and we needed to resolve the author after clicking a link-to helper, then I’m still confused as to the convention for resolving the author before rendering the template. Seems like you would want to access the author instance via post.get('author').


#4

I want to be able to remove an author without removing his name from the posts. I know this is not the belongsTo convention. You can still select the removed author in the selectbox this way. See http://exmer.com/bloggrcouch/ I just posted it because it shows how to load a related model.