0
votes

I'm trying to load an edit form in a nested route. When I hit the URL for editing a company, /companies/87/edit, I get the edit template but instead of a single model I get: DS.RecordArray:ember314. It's an array with the one model object I want.

I noticed that ember314 is the same array getting used in the model of the companies template. I tried having CompanyEditRoute's model return {} and I can see that change reflected on the template. I'm not sure how to get only the model I want, not inside of an array.

EDIT

It looks like the nested routes are just picking up the model array off of the main route. This is strange because I confirmed each Route's model method is being called when the appropriate URL is hit.

App.Router.map(function() {
  this.resource('companies', function() {
    this.resource('company', { path: ':company_id' }, function() {
      this.route('edit');
    });
  });  
});


App.CompanyEditRoute = Ember.Route.extend({
  model: function(params) {
    return App.Company.find(params.company_id);
  },

  renderTemplate: function() {
    this.render({into: 'application', outlet: 'modal'});
  }
});
1
What is the value of params.company_id. I suspect it's undefined so you're basically calling App.Company.find() again, which is probably also your model for 'companines'Trek Glowacki
You are correct! How would I access the company_id in my edit route in this case?David R
Nice catch Trek, modelFor('company')Kingpin2k
@TrekGlowacki, you were correct and I'm able to get my model now for both routes. Now I'm completely confused about how routes work. Why does CompanyIndexRoute#model require params, they're set to {}, and the model for company is magically accessible for the url /companies/25? I have access to the correct company by id but it's not set anywhere, and I'd expect it to be set on the CompanyIndexRoute since it has the dynamic segment in Router.David R
The parameters get passed only to route that defines them. You can access the parent's model with modelFor('company') or, if you really want to trigger another find, model has two args: params and transition. The transition contains more all the params required to enter this nesting.Trek Glowacki

1 Answers

1
votes

Just a hunch, but when you use render in the renderTemplate hook you aren't specifying the controller, so it might just be throwing away your controller and using the parent controller.

Look up the documentation, but I think it's something like this

renderTemplate: function(controller, model) {
  this.render({into: 'application', outlet: 'modal', controller: controller});
}