简体   繁体   English

EmberJS在嵌套路由中获取动态参数

[英]EmberJS get dynamic parameter in nested route

I am developing a website using Ember JS. 我正在使用Ember JS开发网站。 I have created a nested route like this: 我创建了这样的嵌套路由:

//router
this.resource('store/checkout', {path: '/store/checkout/:order_id'}, function(){
      this.resource('store/checkout-lines', {path: ''});
  });

This results in the route /store/checkout/:order_id calling both routes and corresponding tempaltes. 这导致路由/ store / checkout /:order_id调用了路由和相应的临时模板。 The template for store/checkout has an {{outlet}} for the template store/checkout-lines. 用于商店/结帐的模板具有用于模板商店/结帐行的{{outlet}} In the routes I have this code: 在路线中,我有以下代码:

//store/chekout
export default Ember.Route.extend({
    model: function(params) {
        return this.store.find('order', params.order_id);
    }
});

//store/checkout-lines
export default Ember.Route.extend({
    model: function(params) {
        var order_id = params.order_id; //this does not work
        return this.store.find('order-item', {orderId: order_id});
    }
});

But my problem is that in the route for store/checkout-lines, I cannot get the orderId. 但是我的问题是在商店/结帐行的路线中,我无法获取orderId。 How can I achieve this? 我该如何实现? Or am I at the wrong track and should be doing this in another way? 还是我走错了路,应该以另一种方式这样做? My goal is that the route /store/checkout/:order_id should call the server to fetch both order and orderItems. 我的目标是路由/ store / checkout /:order_id应该调用服务器以获取order和orderItems。

What some people seem to miss is that even if you are visiting a nested route, the model for the parent route is loaded. 有些人似乎想念的是,即使您正在访问嵌套路线,也会加载父路线的模型。 In your nested route, you can easily fetch the model from the parent route using modelFor(type) and then get your information from there. 在嵌套路线中,您可以使用modelFor(type)轻松地从父路线中获取模型,然后从那里获取信息。 In your case it would be like this. 在您的情况下将是这样。

//store/checkout-lines
export default Ember.Route.extend({
    model: function(params) {
        var order_id = this.modelFor('checkout').get('id');
        return this.store.find('order-item', { orderId: order_id });
    }
});

This might seem like an extra step but when you get around to it it really makes a lot of sense and works very well. 这似乎是一个额外的步骤,但是当您熟悉它时,这确实很有意义并且可以很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM