|
2 | 2 | /** */
|
3 | 3 | import { Ng2StateDeclaration } from "./interface";
|
4 | 4 | import {
|
5 |
| - NgModule, OpaqueToken, ModuleWithProviders, ANALYZE_FOR_ENTRY_COMPONENTS, Provider, Type |
| 5 | + NgModule, OpaqueToken, ModuleWithProviders, ANALYZE_FOR_ENTRY_COMPONENTS, Provider, Injector |
6 | 6 | } from "@angular/core";
|
7 | 7 | import { CommonModule, LocationStrategy, HashLocationStrategy, PathLocationStrategy } from "@angular/common";
|
8 | 8 | import { _UIROUTER_DIRECTIVES } from "./directives/directives";
|
9 | 9 | import { UIView } from "./directives/uiView";
|
10 |
| -import { UrlRuleHandlerFn, TargetState, TargetStateDef } from "ui-router-core"; |
| 10 | +import { UrlRuleHandlerFn, TargetState, TargetStateDef, UIRouter } from "ui-router-core"; |
11 | 11 | import { _UIROUTER_INSTANCE_PROVIDERS, _UIROUTER_SERVICE_PROVIDERS } from "./providers";
|
12 | 12 |
|
13 | 13 | // import { ROUTES } from "@angular/router/src/router_config_loader";
|
@@ -189,43 +189,45 @@ export interface StatesModule {
|
189 | 189 | /**
|
190 | 190 | * A UI-Router Module's imperative configuration
|
191 | 191 | *
|
192 |
| - * If a UI-Router Module needs to perform some configuration (such as registering parameter types or Transition Hooks) |
193 |
| - * a `configClass` should be supplied. |
| 192 | + * If a UI-Router Module needs to perform some configuration (such as registering |
| 193 | + * parameter types or Transition Hooks) a `configFn` should be supplied. |
| 194 | + * The function will be passed the `UIRouter` instance and the module's `Injector` |
194 | 195 | *
|
195 |
| - * Mark the class as `@Injectable()` and inject any required dependencies. |
196 |
| - * When the module is being loaded, an instance will be created and injected. |
197 |
| - * |
198 |
| - * Example: |
| 196 | + * #### Example: |
199 | 197 | * ```js
|
200 |
| - * @Injectable() |
201 |
| - * export class MyUIRouterConfig { |
202 |
| - * // The constructor may be injected |
203 |
| - * constructor(uiRouter: UIRouter) { |
204 |
| - * const requireAuthentication = (transition: Transition) => { |
205 |
| - * let injector = transition.injector(); |
206 |
| - * if (!injector.get(AuthService).isAuthenticated()) { |
207 |
| - * return uiRouter.stateService.target('login'); |
208 |
| - * } |
209 |
| - * } |
210 |
| - * |
211 |
| - * uiRouter.transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthentication); |
212 |
| - * |
213 |
| - * let builtInStringType = urlMatcherFactory.type('string'); |
214 |
| - * let slugType = Object.assign({}, builtInStringType, { encode: (str) => str, decode: (str) => str }); |
215 |
| - * uiRouter.urlMatcherFactory.type('slug', slugType); |
216 |
| - * } |
| 198 | + * import { Injector } from "@angular/core"; |
| 199 | + * import { UIRouter } from "ui-router-ng2"; |
| 200 | + * import { requireAuthHook } from "./requireAuthHook"; |
| 201 | + * import { MyService } from "./myService"; |
| 202 | + * |
| 203 | + * export function configureMyModule(uiRouter: UIRouter, injector: Injector) { |
| 204 | + * // Get UIRouter services off the UIRouter object |
| 205 | + * let urlConfig = uiRouter.urlService.config; |
| 206 | + * let transitionService = uiRouter.transitionService; |
| 207 | + * uiRouter.trace.enable("TRANSITION"); |
| 208 | + * |
| 209 | + * transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthHook); |
| 210 | + * |
| 211 | + * // Create a slug type based on the string type |
| 212 | + * let builtInStringType = urlConfig.type('string'); |
| 213 | + * let slugType = Object.assign({}, builtInStringType, { encode: (str) => str, decode: (str) => str }); |
| 214 | + * urlConfig.type('slug', slugType); |
| 215 | + * |
| 216 | + * // Inject arbitrary services from DI using the Injector argument |
| 217 | + * let myService: MyService = injector.get(MyService) |
| 218 | + * myService.useFastMode(); |
217 | 219 | * }
|
218 | 220 | * ```
|
219 | 221 | *
|
220 | 222 | * ```js
|
221 | 223 | * @NgModule({
|
222 | 224 | * imports: [
|
223 |
| - * UIRouterModule.forChild({ states: STATES, configClass: MyUIRouterConfig }); |
| 225 | + * UIRouterModule.forChild({ states: STATES, config: configureMyModule }); |
224 | 226 | * ]
|
225 | 227 | * })
|
226 | 228 | * class MyModule {}
|
227 | 229 | * ```
|
228 | 230 | */
|
229 |
| - configClass?: Type<any>; |
| 231 | + config?: (uiRouterInstance: UIRouter, injector: Injector) => any; |
230 | 232 | }
|
231 | 233 |
|
0 commit comments