angular-ui/ui-router — pull request #124 by stu-salsbury (2013-05-08 01:06:33+00:00)
Title: add feature to clearAll and (re)initialize providers to support on-the-fly routing changes

Background: it would be beneficial to be able to modify routing during runtime. For example, when a user is logged into a system, the routes should be different than when the user is not logged in. In another example, imagine a CMS. The admin user can add a blog feature to the CMS. In order to preview the blog, a new route should be defined so that the blog can be seen. Other cases exist where the ability to define and undefine routes would be useful.

I looked at being able to modify routes/states on the fly in ui-router. There are some challenges.

(1) It doesn't expose the route definition functions in the services. This is easily surmountable. In fact, this pull request contains a test spec that uses a service to access the providers. So *no big deal there*, but it would be nice fold this into ui-router at some point.

(2) The ui.state and ui.compat modules do not keep track of the rules that are defined for them in urlRouter. The challenge here is that if you want to pick out a state or a "route when" that you want to modify or remove, it would be unwieldy at best to edit the rules in the urlRouter accordingly. It would be great if those two packages could track which rules go with which states and routes so that they could be modified or removed later.

(3) Related to challenge (2), the "otherwise" rule is appended to the end of the rules array when the urlRouter service starts. This complicates one potential solution to (2), which would be to track the index position of the rule that relates to a state or when route.

So, in the long run, I'd like to solve these three challenges. I hope that such a feature is welcome in ui-router and I'd appreciate some feedback on it.

In the short run, this pull request adds clearAll and init methods to the providers so that an app can wipe out the routes and redefine them. Because the otherwise rule needs to remain last, the URL provider is very slightly modified to use init() when starting the service.

The test spec uses the technique of exposing the providers to a service so that they can be used at runtime.

Feedback is welcome. I hope clearAll/init can be supported so that people will be able to use my larger project which is to manage both routes *and a generically applicable navigational UI directive* in a datadriven manner. More on that later.

Thanks!