-
Notifications
You must be signed in to change notification settings - Fork 28
dev: RouteFlattenerService
RouteFlattenerService
is our workaround for Angular not inheriting all route parameters.
We use nested routes quite a lot (example: projects/1/reports/2/applications/3
). Angular provides Resolver
to resolve data from route parameters, so Resolver
can load project with id 1
, execution information for execution with id 2
and application information about application 3
. All these data might be (and probably will be) useful for all routed components in component-tree. But Angular has some quirks, for example component sees only parameters from its route, not from parent routes, each component gets different route object (which is kind of stupid, since the URL is the same in all cases...) and many more.
To overcome all those issues we use RouteFlattenerService
. Most important method is getFlattenedRouteData
which accepts ActivatedRouteSnapshot
and resolves it into flat object and kind of emulates parameters and data inheritance. To simplify our code, we have OnFlatRouteLoaded
observable and onNewRouteActivated
method. AppComponent
listens to Router
events and for each NavigationEnd
event and executes onNewRouteActivated
method which triggers OnFlatRouteLoaded
Observable.
Most of our routed components inherit from RoutedComponent
and subscribe to OnFlatRouteLoaded
(or flatRouteLoaded
) Observable in ngOnInit()
method (in newer versions it will be in initialize()
method called from constructor, since Angular changed order of Router events and broke our code).
It seems Angular team finally realized it would be useful to always inherit route parameters and came with this PR. If it is implemented correctly and gets merged, we could potentially remove all RouteFlattener
related code and replace it by simple subscribing to ActivatedRoute
params/data observables.