0
votes

I am trying to set model of a route(not nested) but with relationship belong to with setupController

Here is my JSBIN i.e company hasMany Contacts from companies list page I have directly linked to contacts page (want flow that ways)

I want to use the company model to get its contacts list but I am getting error

Error while loading route: TypeError: Cannot call method 'resolve' of undefined

Heres my Router

App.Router.map(function() {
  this.resource('companies',{path:'/'});
  this.resource('company',{path:'/company/:company_id'});
  this.resource('contacts',{path:'/company/:id/contacts'});  
});

This is what I am doing

App.ContactsRoute = Em.Route.extend({
  setupController: function(controller, model) {
    console.log(model.get('contacts')); //This line gives error
    //controller.set('model',model.get('contacts'));
  }
});

The MODEL

App.Company = DS.Model.extend({
    name: DS.attr('string'),
    contacts: DS.hasMany('Contact')
});

App.Company.FIXTURES = [{
    id: 1,
    name: 'company 1',
    contacts: ['1']
}, {
    id: 2,
    name: 'company 2',
    contacts: ['1', '2']

}];

App.Contact = DS.Model.extend({
    name: DS.attr('string'),
    company: DS.belongsTo('Company')

});
App.Contact.FIXTURES = [{
    id: 1,
    name: 'employee 1',
    company: 1   
}, {
    id: 2,
    name: 'employee 2',
    company: 2
}];
1

1 Answers

1
votes

Updated Fiddle

I updated the fiddle for you and changed a few things. To me, it seemed more logical to add contacts as a route of the resource company instead of a separate resource.

I also made the company model async, so it waits for all models to be loaded before actually rendering everything

Take a look and feel free to ask questions if you don't fully understand the solution.