Ember 嵌套全局路由(通配符)
Ember nested global routes (Wildcard)
我正在尝试在我的路线中做这样的事情:
this.route('products', { path: "/products/*choises"}, function() {
this.route('promotion', {path: "/promotion/*offers"});
});
产品路线:
offerPath: function(params){
this.transitionTo('product.promotion', params);
}
问题是我访问的促销并不重要,应用认为是产品路线的一部分。
我怎样才能做到这一点?我需要它们嵌套。
更新:
您可以在路由器中使用 beforeModel(transition)
挂钩来检查 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
,那么你将可以访问带有 [=20] 的字符串=] 在子路径级别。
我正在尝试在我的路线中做这样的事情:
this.route('products', { path: "/products/*choises"}, function() {
this.route('promotion', {path: "/promotion/*offers"});
});
产品路线:
offerPath: function(params){
this.transitionTo('product.promotion', params);
}
问题是我访问的促销并不重要,应用认为是产品路线的一部分。 我怎样才能做到这一点?我需要它们嵌套。
更新:
您可以在路由器中使用 beforeModel(transition)
挂钩来检查 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
,那么你将可以访问带有 [=20] 的字符串=] 在子路径级别。