我使用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
重要嗎? –