erikringsmuth / app-router

Router for Web Components
https://erikringsmuth.github.io/app-router/
MIT License
611 stars 83 forks source link

Better support for dynamically adding routes #2

Closed ChrisMcKenzie closed 10 years ago

ChrisMcKenzie commented 10 years ago

adding routes dynamically works great unless you are currently at the route that is being added

erikringsmuth commented 10 years ago

I hadn't thought of dynamically added routes yet. Could you throw together a gist showing how you want to use it?

ChrisMcKenzie commented 10 years ago

Say I have an app the that gets it's route definitions from a remote source, I would grab the definitions (using core-ajax or jQuery or plain ol' js XHR) and create app-routes in the router after their definitions have been loaded.

if my incoming route is say /home and my current location is the same it should just set that route active imediately.

My thoughts for accomplishing this are: I don't particularly care for the idea of something like a change watcher as it adds unnecessary complexity (and I love the simplicity of this router) but if it had one of the two ability below it would serve my needs perfectly.

  1. a way to manually call changeActiveRoute (currently blocked by a previous state check app-router.html:50)
  2. added an auto attribute which would have your current behavior and with out it you could manually bootstrap the router. ( a simple way to hold off routing until ready )
erikringsmuth commented 10 years ago

Ah, I see! I should really come up with a better way to do that previous state check. I added it because I listen for popstate and hashchange events and browsers are inconsistent whether they trigger one or both.

I think I could do a few things.

Let me see if I can figure out the first option. Otherwise I'll go for one of the later two.

erikringsmuth commented 10 years ago

I played around with it and went with a variation that automatically initializes and can be manually initialized by setting init="manual" then calling router.init() later. This will keep the API as simple as possible for people not even touching the Javascript.

https://github.com/erikringsmuth/app-router#init

I also changed changeActiveRoute to go. That was unrelated, I just think it'll be a shorter name to call when I get the previous state issue worked out.

Thanks for the PR!