Skip to content

Commit

Permalink
feat(UIRouterConfig): Define UIRouterConfig class for router bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Apr 1, 2016
1 parent ff54d61 commit c16b9e6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/ng2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import "./justjs";

export * from "./ng2/providers";
export * from "./ng2/directives";
export * from "./ng2/uiRouterConfig";

27 changes: 19 additions & 8 deletions src/ng2/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,29 @@ import {ViewService} from "../view/view";
import {UiView} from "./uiView";
import {ng2ViewsBuilder, Ng2ViewConfig} from "./viewsBuilder";
import {Ng2ViewDeclaration} from "./interface";
import {UIRouterConfig} from "./uiRouterConfig";

export const UIROUTER_PROVIDERS: Provider[] = [
let uiRouterFactory = (routerConfig: UIRouterConfig) => {
let router = new UIRouter();

router.viewService.viewConfigFactory("ng2", (node: Node, config: Ng2ViewDeclaration) => new Ng2ViewConfig(node, config));
router.stateRegistry.decorator('views', ng2ViewsBuilder);

router.stateRegistry.stateQueue.autoFlush(router.stateService);

provide(UIRouter, { useFactory: () => {
let router = new UIRouter();
routerConfig.configure(router);

router.viewService.viewConfigFactory("ng2", (node: Node, config: Ng2ViewDeclaration) => new Ng2ViewConfig(node, config));
router.stateRegistry.decorator('views', ng2ViewsBuilder);
router.stateRegistry.stateQueue.autoFlush(router.stateService);
if (!router.urlRouterProvider.interceptDeferred) {
router.urlRouter.listen();
router.urlRouter.sync();
}

return router;
};

export const UIROUTER_PROVIDERS: Provider[] = [

return router;
} }),
provide(UIRouter, { useFactory: uiRouterFactory, deps: [UIRouterConfig] }),

provide(StateService, { useFactory: (r: UIRouter) => { return r.stateService; }, deps: [UIRouter]}),

Expand Down
79 changes: 79 additions & 0 deletions src/ng2/uiRouterConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {UIRouter} from "../router";
/**
* Provides states configuration to UI-Router during application bootstrap.
*
* An instance of this class should be `provide()`d to the application `bootstrap()`.
*
* @example
* ```js
* import {UIROUTER_PROVIDERS, UiView} from "ui-router-ng2";
* import {MyConfig} from "./app/myConfig";
*
* bootstrap(UiView, [
* ...UIROUTER_PROVIDERS,
* provide(UIRouterConfig, { useClass: MyConfig }
* ]);
* ```
*
* The application's initial states should be registered with the [[UIRouter.stateRegistry]].
* Any global configuration (transition hooks, parameter types, etc) should be done here.
*
* @example
* ```js
*
* // myconfig.ts
* import {STATES} from "./states";
* import {registerAuthHook} from "./hooks";
* import {registerSlugType} from "./paramtypes";
*
* export class MyConfig {
* configure(uiRouter: UIRouter) {
* STATES.forEach(state => uiRouter.stateRegistry.register(state));
* registerAuthHook(uiRouter.transitionService);
* registerSlugType(uiRouter.urlMatcherFactory);
* }
* }
*
* // states.ts
* import {FooComponent} from "./foo.component";
* import {BarComponent} from "./bar.component";
* import BAZ_MODULE_STATES from "./baz/states";
*
* export let STATES = [
* { name: 'foo', url: '/url', component: FooComponent},
* { name: 'bar', url: '/bar', component: BarComponent}
* ].concat(BAZ_MODULE_STATES);
*
* // hooks.ts
* export function registerAuthHook(transitionService: TransitionService) {
* let requireAuthentication = ($state, AuthService) {
* if (!AuthService.isAuthenticated()) {
* return $state.target('login');
* }
* }
* transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthentication);
* }
*
*
* // paramtypes.ts
* export function registerSlugType(urlMatcherFactory: UrlMatcherFactory) {
* let builtInStringType = urlMatcherFactory.type('string');
* let slugType = Object.assign({}, builtInStringType, { encode: (str) => str, decode: (str) => str });
* urlMatcherFactory.type('slug', slugType);
* }
* ```
*
*/
export class UIRouterConfig {
/**
* Configures UI-Router before bootstrap
*
* An app should perform UI-Router configuration here, such as registering the initial set of states,
* parameter types, defining global hooks, etc.
*
* @param uiRouter the uiRouter instance being configured
*/
public configure(uiRouter: UIRouter) {

}
}
5 changes: 4 additions & 1 deletion src/url/urlRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ function update(rules: Function[], otherwiseFn: Function, evt?: any) {
* in your module config.
*/
export class UrlRouterProvider {
/** @hidden */
rules = [];
/** @hidden */
otherwiseFn: Function = null;
private interceptDeferred = false;
/** @hidden */
interceptDeferred = false;

constructor(private $urlMatcherFactory: UrlMatcherFactory, private $stateParams: StateParams) {

Expand Down

0 comments on commit c16b9e6

Please sign in to comment.