2

In some case I got an issue with the routing url. Here's my router :

contacts: Em.Route.extend({
    route: '/contacts',

    index: Em.Route.extend({
        route: '/',
        connectOutlets: function(router, context) {
            App.contactsController.populate()
            var appController = router.get('applicationController');
            appController.connectOutlet('contactsList');
        }
    }),

    show: Em.Route.extend({
        route: '/:contactid',
        connectOutlets: function(router, context) {
            alert('show contact');
        }
    }),

    doShowContact: function(router, event){
        router.transitionTo('show', {contactid: event.context.id});
    }
}),

When I enter inside doShowContact, if I specify 'contactid' as context and '/:contactid' as route inside 'show', I'll get for example '/contacts/3' in the browser url, everything is ok.

However in doShowContact, if I specify 'contact_id' rather than 'contactid' as context and '/:contact_id' rather than '/:contactid' as route. I'll get '/contacts/undefined' in the browser url.

Is there a way to explain it ? Thanks!

sly7_7
  • 11,961
  • 3
  • 40
  • 54
ThomasDurin
  • 1,943
  • 2
  • 14
  • 20

1 Answers1

4

You should simply pass the contact instance, not forge an object with contactid property:

doShowContact: function(router, event) {
  var contact = event.context;
  router.transitionTo('show', contact);
}

You should also specify the modelClass property in your route:

show: Em.Route.extend({
  route: '/:contact_id',
  modelClass: App.Contact,

  // ...
})
Mike Aski
  • 9,180
  • 4
  • 46
  • 63
  • Thanks a lot Mike. It works better now. Is it important to specify a modelClass in the route ? – ThomasDurin Jul 27 '12 at 12:22
  • 1
    The class will be used for model instance lookup (ember-data model's find) – Mike Aski Jul 27 '12 at 15:46
  • 4
    @Thomas the model class will is extracted from the name of the parameter: `user_id` will assume the model `YourNamespace.User` whereas `repository_id` will asume `YourNamespace.Repository`. If you use this convention, specifying the `modelClass` explicitly is not required. – pangratz Jul 27 '12 at 15:51