Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build] Replace $routeProvider with ui-router #4

Closed
joshdmiller opened this issue Mar 4, 2013 · 4 comments
Closed

[build] Replace $routeProvider with ui-router #4

joshdmiller opened this issue Mar 4, 2013 · 4 comments
Milestone

Comments

@joshdmiller
Copy link
Member

No description provided.

@rolaveric
Copy link

I had a go at implementing this (ce041e6553), and it works pretty well.

However, I found I had to duplicate my header and footer views between the home and about features - they couldn't inherit them from the ngBoilerplate module because the module inheritance goes in the opposite direction (ie. ngBoilerplate inherits from home and about, not the other way around)
It comes up enough that the ui-router team have added the issue to their FAQ.

The best solution I can think of is to add a new ngBoilerplate.rootState module to src/app/app.js, where it's own job is to declare an abstract state named rootState with the default views.

Thoughts?

joshdmiller pushed a commit that referenced this issue Jun 19, 2013
Instead of $routeProvider, ui-router's $stateProvider is now used for
managing application states and routes. ui-router is *much* more
powerful and for large applications will provide many benefits that the
boilerplate does not explore in vitro.

Closes #4.
@joshdmiller
Copy link
Member Author

I added ui-router as a drop-in replacement and it works swimmingly. It's on branch v0.3wip for the moment but will be merged to master within the next few days. So I'm going to close this issue.

@rolaveric I didn't really follow that. I know this was a month ago, but would you mind elaborating?

@rolaveric
Copy link

@joshdmiller Sorry - I'm sure it made perfect sense to me at the time, but it looks like I lost the plot part way through.

I was trying to use the nested states and views that ui-router provides, splitting index.html to have 3 ui-view directives - header, content, and footer - and wrap them all up under a state called (creatively) rootState.
The trouble is that I had created a dependency between the child modules (ngBoilerplate.home and ngBoilerplate.about) and the root ngBoilerplate module, rather than the other way around.
ie. Before state about can be loaded into $stateProvider, it needs the rootState state to have been loaded first, otherwise ui-router throws an error.

My previous solution, which I mentioned in my last comment, was create a separate ngBoilerplate.rootState module which solely handles initialising the root state for the application, and can then be passed around to the child pages to attach to.
That was a terrible idea because it means you can't just pick up those child pages and put them in another application without rewriting it.

A better solution (Which I will push to my fork when I get home this evening) is to simply never call $stateProvider.state() within your components - only in your application.
Instead just declare them in a module.provider() that your application can request and initialise at will.
eg:

// Component that could be used by any application
angular.module('myComponent', []).provider('myComponentStates', function () {
  // Provider boilerplate
  var provider = this;
  provider.$get = function () {return provider;};

  // Declare my states as objects
  provider.states = [
    {name: 'myComponent', template: 'myComponent.tpl.html'},
    {name: 'myComponent.childState' /* ... */}
  ];

  // Specify what state should be attached when including this module
  provider.rootState = provider.states[0];
});

// Application which wants to use 'myComponent' under route "/feature1"
angular.module('myApp', ['ui.states', 'myComponent']).
config(['$stateProvider', 'myComponentStatesProvider', function ($stateProvider, myComponentStatesProvider) {
  // Declare my root page state, with headers and footers, etc.
  var pageState = {name: 'page', /* ... */};

  // Attach the rootState for mycomponent
  myComponentStatesProvider.rootState.parent = pageState;

  // Set it's URL to 'feature1'
  myComponentStatesProvider.rootState.url = '/feature1';

  // Now you can safely initialise all the states
  $stateProvider.state(pageState);
  angular.forEach(myComponentStates.states, $stateProvider.state);
}])

There's a discussion on how to build this into ui-router natively, but there hasn't been any activity for about 2 weeks:
angular-ui/ui-router#160 (comment)

@rolaveric
Copy link

Here's a commit for my 'better solution'(tm): 9ef2e62
I realised a provider is overkill and that a constant would work just as well.

I also realised that the latest tagged version of the ui-route directive doesn't support ui-router, but it does in the master branch.

nyarly added a commit to LRDesign/reasoning that referenced this issue May 16, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants