Skip to content

Commit ea4574d

Browse files
authored
feat: create OnUiParamsChanged, OnUiExit interfaces (#800)
* Fixes #788: Create OnUiParamsChanged, OnUiExit interfaces instead of Ng2Component * Deprecated the NG2Component interface. Co-authored-by: stnor <[email protected]>
1 parent 55de08a commit ea4574d

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

src/directives/uiView.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from '@uirouter/core';
3737
import { Ng2ViewConfig } from '../statebuilders/views';
3838
import { MergeInjector } from '../mergeInjector';
39-
import { Ng2Component } from '../interface';
4039

4140
/** @hidden */
4241
let id = 0;
@@ -211,7 +210,7 @@ export class UIView implements OnInit, OnDestroy {
211210
* For each transition, checks if any param values changed and notify component
212211
*/
213212
private _invokeUiOnParamsChangedHook($transition$: Transition) {
214-
const instance: Ng2Component = this._componentRef && this._componentRef.instance;
213+
const instance = this._componentRef && this._componentRef.instance;
215214
const uiOnParamsChanged: TransitionHookFn = instance && instance.uiOnParamsChanged;
216215

217216
if (isFunction(uiOnParamsChanged)) {

src/interface.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** */
33

44
import { StateDeclaration, _ViewDeclaration, Transition, HookResult } from '@uirouter/core';
5-
import { Type, Component } from '@angular/core';
5+
import { Component, Type } from '@angular/core';
66
import { ModuleTypeCallback } from './lazyLoad/lazyLoadNgModule';
77

88
/**
@@ -286,6 +286,89 @@ export interface Ng2ViewDeclaration extends _ViewDeclaration {
286286
bindings?: { [key: string]: string };
287287
}
288288

289+
export interface UiOnParamsChanged {
290+
/**
291+
* A UI-Router view has an Angular `Component` (see [[Ng2ViewDeclaration.component]]).
292+
* The `Component` may define component-level hooks which UI-Router will call at the appropriate times.
293+
* These callbacks are similar to Transition Hooks ([[IHookRegistry]]), but are only called if the view/component is currently active.
294+
*
295+
* The uiOnParamsChanged callback is called when parameter values change.
296+
*
297+
* This callback is used to respond dynamic parameter values changing.
298+
* It is called when a transition changed one or more dynamic parameter values,
299+
* and the routed component was not destroyed.
300+
*
301+
* It receives two parameters:
302+
*
303+
* - An object with (only) changed parameter values.
304+
* The keys are the parameter names and the values are the new parameter values.
305+
* - The [[Transition]] which changed the parameter values.
306+
*
307+
* #### Example:
308+
* ```js
309+
* @Component({
310+
* template: '<input type="text">'
311+
* })
312+
* class MyComponent {
313+
* uiOnParamsChanged(newParams: { [paramName: string]: any }, trans: Transition) {
314+
* Object.keys(newParams).forEach(paramName => {
315+
* console.log(`${paramName} changed to ${newParams[paramName]}`)
316+
* });
317+
* }
318+
* }
319+
* ```
320+
*/
321+
uiOnParamsChanged(newParams: { [paramName: string]: any }, trans?: Transition): void;
322+
}
323+
324+
export interface UiOnExit {
325+
/**
326+
* A UI-Router view has an Angular `Component` (see [[Ng2ViewDeclaration.component]]).
327+
* The `Component` may define component-level hooks which UI-Router will call at the appropriate times.
328+
* These callbacks are similar to Transition Hooks ([[IHookRegistry]]), but are only called if the view/component is currently active.
329+
*
330+
* The uiCanExit callback is called when the routed component's state is about to be exited.
331+
*
332+
* The callback can be used to cancel or alter the new Transition that would otherwise exit the component's state.
333+
*
334+
* This callback is used to inform a view that it is about to be exited, due to a new [[Transition]].
335+
* The callback can ask for user confirmation, and cancel or alter the new Transition. The callback should
336+
* return a value, or a promise for a value. If a promise is returned, the new Transition waits until the
337+
* promise settles.
338+
*
339+
* Called when:
340+
* - The component is still active inside a `ui-view`
341+
* - A new Transition is about to run
342+
* - The new Transition will exit the view's state
343+
*
344+
* Called with:
345+
* - The `Transition` that is about to exit the component's state
346+
*
347+
* #### Example:
348+
* ```js
349+
* @Component({
350+
* template: '<input type="text">'
351+
* })
352+
* class MyComponent {
353+
* dirty = true;
354+
*
355+
* constructor(public confirmService: confirmService) {
356+
*
357+
* }
358+
*
359+
* uiCanExit(newTransition: Transition) {
360+
* if (this.dirty && newTransition.to() !== 'logout') {
361+
* return this.confirmService.confirm("Exit without saving changes?");
362+
* }
363+
* }
364+
* }
365+
* ```
366+
*
367+
* @return a hook result which may cancel or alter the pending Transition (see [[HookResult]])
368+
*/
369+
uiCanExit(newTransition?: Transition): HookResult;
370+
}
371+
289372
/**
290373
* The shape of a controller for a view (and/or component), defining the controller callbacks.
291374
*
@@ -294,6 +377,8 @@ export interface Ng2ViewDeclaration extends _ViewDeclaration {
294377
* These callbacks are similar to Transition Hooks ([[IHookRegistry]]), but are only called if the view/component is currently active.
295378
*
296379
* This interface defines the UI-Router component callbacks.
380+
*
381+
* @deprecated This interface has been replaced by UiOnExit and UiOnParamsChanged.
297382
*/
298383
export interface Ng2Component extends Component {
299384
/**

0 commit comments

Comments
 (0)