Is this an accepted (not private) way to get the controller from a Transition?

#1

Today, I came across an issue with an add-on I am using. When digging around I noticed that a refactoring towards newer ember versions has been the cause of this. Previously the HandlerInfo of the transition has been used and the replacement to use the RouteInfo seemed to be the cause of the issue.

The only objective is to get a property from the currently active controllers. After some debugging I came up with this solution:

transition.intent.router.currentRouteInfos.some((routeInfo) => 
   get(routeInfo, 'route.controller.preserveScrollPosition')
);

However, I was wondering if this solution is relying on private api we should not be using. For instance the intent property does not seem to be documented for the transition class - not even within private fields.

This is my PR in case additional information is required.

1 Like
#2

transition.intent is not public API.

We now have transition.from and transition.to which point at linked lists of RouteInfos that describe all the routes that we’re leaving and entering. See RFC 95.

But it’s true that the RouteInfos don’t say anything directly about the controllers, mostly because it’s almost always a mistake for people to try to manipulate controllers from here.

You can find the controller yourself based on the name of the route, something like:

let enteringRouteName = transition.to.name;
let enteringController = getOwner(this).lookup(`controller:${enteringRouteName}`);

This would work as long as people aren’t overriding which controller goes with their route, which is slated for deprecation and removal anyway.

To keep compatibility with existing use cases, router-scroll may need to continue using private API until it can migrate users into new patterns (either making it clear they’re not allowed to use the deprecated render methods to customize the choice of controller, or moving the preserveScrollPosition API from controllers to routes (it really does seem to be a concern of routes anyway, to me).

2 Likes
#3

@ef4 thanks a lot for your reply. That clears things up :slight_smile: