Ember.js: Using this.transitionTo skips loading the model

Created on 22 Dec 2013  ·  3Comments  ·  Source: emberjs/ember.js

I'm not sure whether this is intentional behavior but let me explain. If I use a transitionTo on a route that has dynamic segments the model method on the destination route never gets called. Essentially I'm just passing through a params with an id and I still want to load the right model.

If I refresh the page with the right URL though the model gets loaded just fine. I put together a codepen to demonstrate.

http://codepen.io/anon/pen/LsfhB

The current version will log out to the console "I'm here." If you look at IndexRoute there is a commented out transition. If that is enabled the "I'm here" never triggers.

Is this expected? If so, where should I be loading my model for the confirmation route?

Most helpful comment

If you change the transition to be this.transitionTo('confirm', 1); then it'll work as you expect.

IIRC if you pass primitives (string, number) to transition instead of a full model then it figures that's an ID and uses it to build the params for the call to model , but if you pass anything complex through (in this case an object) then it treats that as you having passed the model and so doesn't call the model hook.

The API docs could probably do with an example of passing IDs in instead of full models: http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo

Hope that makes sense

All 3 comments

If you change the transition to be this.transitionTo('confirm', 1); then it'll work as you expect.

IIRC if you pass primitives (string, number) to transition instead of a full model then it figures that's an ID and uses it to build the params for the call to model , but if you pass anything complex through (in this case an object) then it treats that as you having passed the model and so doesn't call the model hook.

The API docs could probably do with an example of passing IDs in instead of full models: http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo

Hope that makes sense

Makes sense now. My initial gut question after your comment was how do you pass in multiple parameters? But it looks like you just pass them in as separate arguments.

this.transitionTo('confirm.url', 1, 'the-slug-I-want');

That works well for me.

Question here, if you have a route with subroutes all with dynamic segment:
e.g.
this.route('organization', { path: 'organizations/:organization_id' }, function() {
this.route('project', { path: 'projects/:project_id' }, function() {
this.route('screen', { path: 'screens/:screen_id' });
});
and I am in another route e.g. auth route. When i go to this.transitionTo('organization.project.screen',
{organization_id: 'grege23-234-4c3e-96fe-234423' },
{project_id: '234234-23423-4b99-a492-234' },
{screen_id: '34534-48f4-471d-ad17-3'45 });
I see nothing, because model is not loaded.
How could you go for this one ?

Was this page helpful?
0 / 5 - 0 ratings