Emberjs route with args fail in some case

不打扰是莪最后的温柔 提交于 2019-12-12 18:52:29

问题


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!


回答1:


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,

  // ...
})


来源:https://stackoverflow.com/questions/11684615/emberjs-route-with-args-fail-in-some-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!