user3568719
user3568719

Reputation: 1056

Ember nested route. Load more models

JsBin http://jsbin.com/EveQOke/153/

I know how to load more then one model to a route, using Ember.RSVP.hash. (see Jsbin Children menu). I use dynamic part to access one elem from a collection children/1. But i cant load more models to a nested resource.

In my example i want to populate all the toys for a select, not just list the toys of the child.

I have tried to access the model of the route children

App.ChildRoute = Ember.Route.extend({
  model: function(param){
    return Ember.RSVP.hash({   
      allToys: this.modelFor("children"),
      child:this.store.find('child', param.child_id)
    });

  }
});

and use its model's toy property (since there have already loaded all of the toys)

child.hbs

<h4>All avaiable toys</h4>
<table>
{{#each toy in model.allToys.toys}}
  <tr>
    <td>{{toy.name}}</td>
  </tr>
{{/each}}
</table>

Upvotes: 1

Views: 269

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

The first problem here is your attempting to grab the model from children, but you haven't defined a children route, you defined a childrenIndex route. And if you were to navigate directly to the child route the childrenIndex route wouldn't be instantiated. It's only hit when you hit the children route and no deeper. So either you'll need to move the logic from the childrenIndex to children route, or just use all on the store, which doesn't fetch any new data, just returns all of the different types of toys in the store.

this.store.all('toy')

The secondary problem you have is when you use link-to and send a model, it skips the model hook, which is a problem for dynamic routes (where you define :id) and you're expecting it to return multiple models, this is discouraged on this type of route, and recommended that you set up additional properties on the controller in setupController

App.ChildRoute = Ember.Route.extend({
  model: function(param){
    this.store.find('child', param.child_id);
  },
  setupController: function(controller, model){
    this._super(controller, model); //default implementation of setupController
    controller.set('allToys', this.store.all('toy'));
  }
});

http://jsbin.com/EveQOke/155/edit

Serialize

serialize: function(model){
  return {child_id:model.get('id')};
}

http://jsbin.com/EveQOke/163/edit

Upvotes: 1

Related Questions