更新:
您可以在路由器中使用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/flawless
// console log would be: "manufacturer-209/series-881/tag-17143/none/494822/flawless"
}
});
至少你有剩下的 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') 的字符串。