我使用Ember> = 2.13.x.Ember.js 2,transitionTo在第一级路由中使用多个动态段
我需要使用transitionTo
从路由操作中另一条路线走。
这里是演示烬,颤声:https://ember-twiddle.com/3cbb806f31f8d71a6c414452f4fc9ded
我有这种情况:
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
});
路线/ application.js中:
actions: {
goToPost() {
let post = this.store.findRecord('post', 1);
this.transitionTo('post', post);
}
}
条
路线/ 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 };
}
模板/ application.hbs:
<button type="button" {{action 'goToPost'}}>GoToPost with action (transitionTo)</button>
所以,当我点击一切正常。
我获取应用动作模型,然后我用this.transitionTo('post', post);
。
在我的post
路线,然后我有serialize()
,我现在阅读,当动态片段存在时,它正在使用URL组成。
我的问题:我用正确的方式?
为什么我不能用这样的:this.transitionTo('post', post.category.id, post.id);
让model()
在post
途径获取模型(从数据库或存储)?
我也试过使用this.transitionTo('post', {category_id: post.category.id, post_id: post.id});
,但显然post
路由model()
不加载任何东西,因为它真的认为对象已经存在。
所以,我只能与serialize()
方法解决这个问题。 这是正确的方式吗?
你在你的类别路线有什么? – sheriffderek
重要吗? –