Pass Data when transitionTo another Route

Hi Everyone,

Recently, I started to use EmberJS., and today I cam across a problem in which i can’t seem to workout the solution.

I have 3 Routes. One is the Parent and two are the Child.

Routes: 
Route Parent
    Route Child A
    Route Child B

From the Route Child A I am performing a transitionTo Route Child B which looks like this:

this.get(‘router’).transitionTo(“Routeparent.RouteChildB”);

My main Goal is to pass the Model from RouteChildA to the RouteChildB when performing the transition. However, it is giving me all sorts of errors when i do this:

this.get(‘router’).transitionTo(“Routeparent.RouteChildB”, model);

“More context objects were passed than there are dynamic segments for the route”.

So, my main question would be how can I pass data or a model when transition to another route?

Thanks

Hi there, your app is expecting a dynamic segment for RouteChildB where likely one doesn’t exist.

In your router.js file, you RouteChildB should be declared as like:

this.route('RouteChildB', {path: '/:route_child_b_id'})

then in the model hook for RouteChildB:

model(params) {
  // do some lookup  with params.route_child_b_id
}

Bear in mind if you pass the model directly (and not by id) as in your sample above, then the model hook will not be called in RouteChildB as you are passing the model already.

Finally, if this code is in a route or controller you don’t need this.get('router').transitionTo, instead you can call this.transitionTo(“Routeparent.RouteChildB”) or this.TransitionToRoute(“Routeparent.RouteChildB”) respectively.

https://guides.emberjs.com/release/routing/specifying-a-routes-model/

Thanks very much. That was exactly it.

1 Like