【问题标题】:Ember nested global routes (Wildcard)Ember 嵌套全局路由(通配符)
【发布时间】:2016-04-12 15:49:43
【问题描述】:

我正在尝试在我的路线中做这样的事情:

this.route('products', { path: "/products/*choises"}, function() {
   this.route('promotion', {path: "/promotion/*offers"});
}); 

产品路线:

    offerPath: function(params){
      this.transitionTo('product.promotion', params);
    }

问题是我访问的促销并不重要,应用程序认为是产品路线的一部分。 我怎样才能做到这一点?我需要它们嵌套。

【问题讨论】:

  • 是选择产品ID吗?
  • 简短的回答是你不能,因为 glob 是贪婪的,并且会匹配所有内容直到路径的尽头,就像你看到的那样。

标签: ember.js routes nested wildcard glob


【解决方案1】:

更新: 您可以在路由器中使用beforeModel(transition) hook 来检查 url 中的内容。

http://example.com/products/manufacturer-209/series-881/tag-17143/none/494822/f‌​无法无天

import Ember from 'ember';

export default Ember.Route.extend({

  beforeModel(transition) {
    console.log(transition.params.products.choises)
   // if you use this url: http://example.com/products/manufacturer-209/series-881/tag-17143/none/494822/f‌​lawless
   // console log would be: "manufacturer-209/series-881/tag-17143/none/494822/f‌​lawless"
  }

});

至少你有剩下的 url 所以,你可以过滤掉重要信息并用this.transitionTo() 重定向到确切的位置。


你可以有以下路线:

http://example.com/products/123/promotions/456

http://example.com/products/awesome_souce/promotions/monday_deal

在第一种情况下,您的路线如下所示:

this.route('product', { path: "/products/:product_id"}, function() {
  this.route('promotion', {path: "/promotions/:promotion_id"});
});

第二种情况,可能是这样的:

this.route('product', { path: "/products/:product_name"}, function() {
  this.route('promotion', {path: "/promotions/:promotion_name"});
});

最后,您的路由处理程序可以下载正确的模型(第一种情况的示例):

// app/routes/product.js
import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.findRecord('product', params.product_id);
  }
});

---

// app/routes/product/promotion.js
import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    // you can get access to the parent route model if you need for the api query
    const product = this.modelFor('product');
    return this.store.findRecord('promotion', params.promotion_id);
  }
});

如果您只需要来自product 路由的参数,而不是返回整个记录,例如您可以只使用return params.product_name,那么您将可以在子路由级别访问带有this.modelFor('product') 的字符串。

【讨论】:

  • 感谢佐尔坦!问题是我的路线更复杂。例如,对于产品:http://example.com/products/manufacturer-209/series-881/tag-17143/none/494822/flawless 对于报价:http://example.com/products/manufacturer-209/series-881/tag-17143/none/494822/flawless/promotions/494822-flawless-working 我已经嵌套了路线,效果很好,但是当我重新加载促销网址时,我在产品路线中的应用程序
  • 谢谢!这就是我正在寻找的
猜你喜欢
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 2014-12-19
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多