I. Do not use "...single route that loads a different template...", would be my suggestion, my answer.
If possible:
Try to step back and reconsider the entire desing and
Try to weaken the sense that our application users are interested in url
.
They are not. And if they really do understand what is url
, address bar... they use it to copy
, send
and paste
... not to investigate its parts...
II. Suggestion: Enforce the usage of the ui-router states:
... UI-Router
is organized around states, which may OPTIONALLY have routes, as well as other behavior, attached...
That means, let's reconsider our application as group/hierarchy of well defined states. They can have url
defined , but do not have to (e.g. error state without url)
III. How can we profit from building our application arround the states?
Separation of concern - should be our aim.
The state is a unit which gathers some view/controllers, resolvers, custom data...
That means, that there could be more states reusing views, controllers, etc. Such states could really differ (same view, different controller). But they are single purpose - they are there to handle some scenarios:
- administration of User/Emplyoee record
- list of User/Employee - information ala PhoneList (just email, phone...)
- Security administration - What are the rights of a User
...
And again, there could be many many states. Having even hundred states won't be peformance issue. These are just definitions, a set of references to other pieces, which should be used ... later... if really needed.
Once we've defined use cases, user stories on the level of the state, we can group them into sets/heirarchies.
These groups could be later presented to different user Roles in a different format (different menu items)
But at the end, we gained lot of freedom and simplified maintainablity
IV. Keep application running and growing
If there are few states, maintainanace does not seem to be an issue. But it could happen, that the applicaiton will succeed. Succeed and grow... inside of its design.
Spliting the sate definitions (as a unit of work) and their hierarchies (which user Role can access which states) would simplify its management.
Appling security outside of the states (Event listeners ala '$stateChangeStart'
) is much more easier, then never ending refactoring of template Providers. Also, the main part of security, should be still be applied on a server, regardless what UI allows
V. Summary:
While there is such a great feature as a templateProvider
, which could do some interesting stuff for us (e.g. here: Changing Navigation Menu using UI-Router in AngularJs)...
... we should not use it for security. That could be implemented as some menu/hierarchy built from existing states, based on current Role. Event listeners should check if user is coming to granted state, but the main check must be applied on a server...