marcoslin / angularAMD

Facilitate use of RequireJS in AngularJS
http://marcoslin.github.io/angularAMD
MIT License
734 stars 171 forks source link

Routing to URLs in modules that haven't been loaded yet #88

Closed jasonmacdonald closed 10 years ago

jasonmacdonald commented 10 years ago

I've been struggling with this issue for a while now. I've got AngularAMD running nicely and even wrapped it into a module loading util to handle a bit of the boiler plate to go from loading a ctrl to loading an entire module (basically handling all the ngLoad+ctrl that is needed to execute the module). The issue I have is when that module registers it's own sub routing (using ui-router). If I need to route to that module from another, and that module hasn't loaded yet, those routes don't exist yet, so I can't use state.go to invoke the URL change. Now, I've hacked something together using url matcher to break down the url to the base module name, and force it to load the module and then retry the route after the module has loaded, and that actually works. But, now I have to rely on knowing the URL of the sub modules routes in order to invoke a URL change (since I can't use the stateManager and it's state name because those haven't loaded yet). And when using inheritance in state names, there's no easy way to compile the proper URL for a state that hasn't been loaded. That lead me to registering all the URLs in another helper that could all be loaded at app start-up, without loading the module itself, but it's starting to get messy and unmanageable. There's got to be a better way.

Any thoughts on dealing with routes in modules that haven't been loaded yet? Is there a way to register all the routes without loading all the other module dependencies up front?

marcoslin commented 10 years ago

I was struggling with exactly the same issue a while ago and I posted the following question on SO:

http://stackoverflow.com/questions/24246880/ui-router-lazy-load-state-using-url

@christopherthielen was kind enough to share his solution in form of "UI-Router Extras".

I will confess that I have not tested his implementation yet as I "solved" my problem by changing the navigation of my app.

I will close this issue for now but do share your experience here.