2017-07-26 4 views
1

私は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);postルートでmodel()は(DBまたはストアから)モデルを取得しましょう:私はこのようなものを使用することはできませんなぜ

また、this.transitionTo('post', {category_id: post.category.id, post_id: post.id});を使用しようとしましたが、明らかにpostルートmodel()には、オブジェクトが既に存在すると考えられるため、何もロードされません。

したがって、この問題はserialize()メソッドでのみ解決できます。 それは正しい方法ですか?

+0

カテゴリルートには何がありますか? – sheriffderek

+0

それは重要ですか? –

答えて

1

デフォルトでは、ルートセグメントフック引数で動的セグメントとqueryParamsを取得します。ここで

model({post_id,category_id}) { 
return this.store.findRecord('post', post_id); 
} 

は私がそのURLを簡単にdistinguisableなりpostのように対応する接頭辞を追加することを好むtwiddle

this.route('category', {path: 'category/:category_id'}); 
this.route('post', {path: 'post/:category_id/:post_id'}); 

です。

transitionToにモデルフックを呼び出したい場合は、引数にオブジェクトまたはモデルを指定します。必要なパラメータは常にstringまたはnumberで指定します。 参照:https://guides.emberjs.com/v2.14.0/routing/defining-your-routes/#toc_dynamic-segments

+0

あなたのひねりの中で 'this.transitionTo( 'post'、1,2)'を使っています。代わりに 'this.transitionTo( 'post'、post.get( 'category.id')、post.id)'を使用します。なぜ私はそれが働いていなかった私のプロジェクトで昨日知らない。どうもありがとう。 –

関連する問題