[英]Ember.js 2, transitionTo using multiple dynamic segments in first level route
I'm using Ember >= 2.13.x. 我正在使用Ember> =2.13.x。
I need to use transitionTo
from a route action to go in another route. 我需要从路线操作中使用
transitionTo
来进入另一条路线。
Here is the demo ember-twiddle: https://ember-twiddle.com/3cbb806f31f8d71a6c414452f4fc9ded 这是演示ember-twiddle: https : //ember-twiddle.com/3cbb806f31f8d71a6c414452f4fc9ded
I have this situation: 我有这种情况:
router.json : router.json :
Router.map(function() {
this.route('home', {path: '/'});
this.route('categories', {path: 'categories'});
this.route('category', {path: ':category_id'});
this.route('posts', {path: 'posts'});
this.route('post', {path: ':category_id/:post_id'}); //I have multiple synamic segments in here
});
routes/application.js : route / application.js :
actions: {
goToPost() {
let post = this.store.findRecord('post', 1);
this.transitionTo('post', post);
}
}
routes/post.js : 路线/ post.js :
model(params) {
return this.store.findRecord('post', params.id);
},
serialize(model) {
console.log('model in serialize():', model);
return { category_id: model.get('category.id'), post_id: model.id };
}
templates/application.hbs : template / application.hbs :
<button type="button" {{action 'goToPost'}}>GoToPost with action (transitionTo)</button>
So when I click everything works as expected. 因此,当我单击时,一切正常。
I fetch the model in application action and then I use this.transitionTo('post', post);
我在应用程序操作中获取模型,然后使用
this.transitionTo('post', post);
. 。
In my post
route then I have serialize()
which, I read now, is using for URL composition when dynamic segments are present. 在我的
post
路线中,我有serialize()
,我现在阅读,当存在动态段时,该serialize()
用于URL组合。
My question: I'm using it the right way? 我的问题: 我使用正确的方式吗?
Why I cannot use something like this: this.transitionTo('post', post.category.id, post.id);
为什么我不能使用这样的东西:
this.transitionTo('post', post.category.id, post.id);
and let model()
in post
route fetch the model (from DB or store)? 并让
post
model()
中的model()从数据库或存储中获取模型?
I also tried to use this.transitionTo('post', {category_id: post.category.id, post_id: post.id});
我也尝试使用
this.transitionTo('post', {category_id: post.category.id, post_id: post.id});
but obviously the post
route model()
doesn't load anything because it really thinks the object is already there. 但是很明显,
post
route model()
不会加载任何东西,因为它确实认为对象已经存在。
So, I can fix this problem with serialize()
method only. 因此,我只能使用
serialize()
方法解决此问题。 Is it the right way? 这是正确的方法吗?
You don't need to override serialize hook, by default you will get the dynamic segments and queryParams in route model hook arguments. 您不需要重写序列化挂钩,默认情况下,您将在路由模型挂钩参数中获得动态段和queryParams。
model({post_id,category_id}) {
return this.store.findRecord('post', post_id);
}
this.route('category', {path: 'category/:category_id'});
this.route('post', {path: 'post/:category_id/:post_id'});
I prefer to add corresponding prefix like post
so that URL will become easily distinguisable. 我更喜欢添加诸如
post
之类post
相应前缀,以使URL变得易于区分。
If you want transitionTo
to call model hook, then provide object or model in the argument always provide the required parameter may be string or number. 如果要
transitionTo
调用模型挂钩,则始终在参数中提供对象或模型,所需的参数可以是字符串或数字。 Refer: https://guides.emberjs.com/v2.14.0/routing/defining-your-routes/#toc_dynamic-segments 请参阅: https : //guides.emberjs.com/v2.14.0/routing/defining-your-routes/#toc_dynamic-segments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.