@johnunclesam wrote:
I’m using Ember >= 2.13.x.
I need to use
transitionTo
from a route action to go in another route.Here is the demo ember-twiddle: https://ember-twiddle.com/3cbb806f31f8d71a6c414452f4fc9ded
I have this situation:
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:
actions: { goToPost() { let post = this.store.findRecord('post', 1); this.transitionTo('post', post); } }
routes/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:
<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);
.In my
post
route then I haveserialize()
which, I read now, is using for URL composition when dynamic segments are present.My question: I’m using it the right way?
Why I cannot use something like this:
this.transitionTo('post', post.category.id, post.id);
and letmodel()
inpost
route fetch the model (from DB or store)?I also tried to use
this.transitionTo('post', {category_id: post.category.id, post_id: post.id});
but obviously thepost
routemodel()
doesn’t load anything because it really thinks the object is already there.So, I can fix this problem with
serialize()
method only. Is it the right way?
Posts: 2
Participants: 2