artch/angular-route-segment

Wait for resolve promises to finish before invoking template?

Opened this issue · 0 comments

I'm currently using resolve to preload resources needed for a view and using untilResolved to display a loading screen (similar to the official example). Using this approach I've got two choices for the template that's displayed once the resource is resolved: success (the default template) or failed (resolveFailed template).

I'd like a bit more finesse; I wanted to be able to select my template dynamically based on the resource that's resolved. Using a function for template seemed like the right way to achieve this. I encountered two problems:

  • While the template function is injectable, the injector doesn't yet know about the resolved resources, so you can't inject them trivially. I did find you could look them up via $routeSegment.chain[n].locals. This seems brittle, though, because it requires knowledge of the global segment structure.
  • This may be the reason the resolved resources aren't known to the injector: the template function is called before waiting for any resolve promises to be fulfilled. This appears to be because the template function itself supports the return of a promise which is added to the locals as $template. The code then waits for $q.all(locals) before finalizing the segment switch, instantiating the controller, etc. As a result, I have to create a template before resources are resolved.

I may be missing a better way of doing things, but it seems to me that it'd be hugely powerful/flexible to have the template functions wait for resolve locals. In fact, since there are a set of params that are used while resolving, and a set of params used if resolving fails, it seems logical that the "success" params would also depend on resolving.