Javascript 路线模型钩子被调用两次而不是一次
我想将不完整的URL重定向到完全正确的URL:Javascript 路线模型钩子被调用两次而不是一次,javascript,redirect,ember.js,ember-data,hook,Javascript,Redirect,Ember.js,Ember Data,Hook,我想将不完整的URL重定向到完全正确的URL: http://localhost/product/12/a-single-pr -> http://localhost/product/12/a-single-product-name 问题是模型钩子被调用两次而不是一次,发出两个相同的请求来检索单个对象。有什么线索吗 routes/product.js import Ember from 'ember'; export default Ember.Route.extend({ aft
http://localhost/product/12/a-single-pr -> http://localhost/product/12/a-single-product-name
问题是模型钩子被调用两次而不是一次,发出两个相同的请求来检索单个对象。有什么线索吗
routes/product.js
import Ember from 'ember';
export default Ember.Route.extend({
afterModel(model, transition) {
let formatted = model.get('formatted');
if (transition.params.product.formatted !== formatted) {
let path = '/product/' + model.id + '/' + formatted;
this.replaceWith(path, model);
}
},
model(params) {
return this.get('store').findRecord('product', params.product_id);
}
});
...
Router.map(function() {
this.route('product', {path: '/product/:product_id/*formatted'});
});
...
router.js
import Ember from 'ember';
export default Ember.Route.extend({
afterModel(model, transition) {
let formatted = model.get('formatted');
if (transition.params.product.formatted !== formatted) {
let path = '/product/' + model.id + '/' + formatted;
this.replaceWith(path, model);
}
},
model(params) {
return this.get('store').findRecord('product', params.product_id);
}
});
...
Router.map(function() {
this.route('product', {path: '/product/:product_id/*formatted'});
});
...
余烬正在按预期工作 点击产品路线,它会获取模型,然后在afterModel中,它会重定向回产品路线,这将再次启动路线生命周期,这意味着它将再次获取模型,然后再次调用afterModel 解决这个问题的另一种方法是替换afterModel中的URL,而不是重定向回同一个路由 要实现以下目标,请参阅此答案: