【问题标题】:Emberjs route with args fail in some case在某些情况下,带有 args 的 Emberjs 路由会失败
【发布时间】:2012-07-25 22:39:04
【问题描述】:

在某些情况下,我遇到了路由 url 的问题。这是我的路由器:

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});
    }
}),

当我进入 doShowContact 时,如果我将 'contactid' 指定为上下文,将 '/:contactid' 指定为“show”内的路由,我会得到例如'/contacts/3' 在浏览器 url 中,一切正常。

但是在 doShowContact 中,如果我指定 'contact_id' 而不是 'contactid' 作为上下文并且 '/:contact_id' 而不是 '/:contactid' 作为路由。我会在浏览器 url 中得到 '/contacts/undefined'

有没有办法解释?谢谢!

【问题讨论】:

    标签: ember.js ember-old-router


    【解决方案1】:

    您应该简单地传递联系人实例,而不是伪造具有contactid 属性的对象:

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

    您还应该在路由中指定modelClass 属性:

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

    【讨论】:

    • 非常感谢迈克。现在效果更好了。在路由中指定modelClass重要吗?
    • 该类将用于模型实例查找(ember-data模型的查找)
    • @Thomas 模型类将从参数名称中提取:user_id 将假定模型 YourNamespace.Userrepository_id 将假定 YourNamespace.Repository。如果使用此约定,则不需要明确指定 modelClass
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2018-04-28
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多