ModelFor Issue in Engine's Routes


#1

Hi all, while working with modelFor in my engine, I got an issue. I have an engine ‘settings’ on my host Application. The settings’s routes.js looks like,

this.route(‘users’, function() { this.route(‘list’, function() { this.route(‘details’, { path: ‘/:user_id’ }, function() { this.route(‘activities’); }); }); });

I’m trying to do this.modelFor(‘users/list/details’) in the model hook of users/list/details/activities route, it’s returning undefined.

I found the reason is that, route.js’s getEngineRouteName method will prepend the Engine name with Route Name(looks like settings.users/list/details). So the Route not found in Engine Instance.

Is there any way to retrieve the model of Parent Route?


#2

usually you wouldn’t want to reference route paths with slashes “/”, the convention is to use dots. So have you tried this.modelFor(‘settings.users.list.details’)?


#3

@dknutsen I have tried this.modelFor(‘users.list.details’) in settings engine :heart_eyes:. It is working fine. Can you give me a difference between ‘/’ and ‘.’ in lookup routes.


#4

I’ll give it a shot. The short answer is that “it’s convention”. A route has two important items in the router map: the name and the path. The name is just an abstract label given to that route, the path defines the part of the URL that it makes up. URLs obviously use / to separate segments, so using slashes to separate names is ambiguous. Ember always expects dots . when separating route names.

So when you are redirecting via a transition or a link-to you’ll (almost) always want to use dots to chain route names. One of the advantages to using names instead of paths for referring to routes is that your paths and therefore your URL structure can change at anytime without affecting your code.

So… try to always think about slashes as relating to URL paths and dots as relating to route name paths. Both can be used to describe the same thing, but a slash suggests it’s a URL and dots suggest that it’s a “named” route path. Typically you will only use named route paths with the dot, and let the framework do the work of managing the URL paths and dynamic segments and such.


#5

Thankyou @dknutsen :heart_eyes: