2015-07-07 22:44:29 +00:00
|
|
|
/**
|
|
|
|
|
* This indirection is needed to free up Component, etc symbols in the public API
|
|
|
|
|
* to be used by the decorator versions of these annotations.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-20 21:28:25 +00:00
|
|
|
import {makeDecorator} from 'angular2/src/core/util/decorators';
|
2015-07-07 22:44:29 +00:00
|
|
|
import {CanActivate as CanActivateAnnotation} from './lifecycle_annotations_impl';
|
2015-08-20 21:28:25 +00:00
|
|
|
import {Promise} from 'angular2/src/core/facade/async';
|
2015-07-17 20:36:53 +00:00
|
|
|
import {ComponentInstruction} from 'angular2/src/router/instruction';
|
2015-07-07 22:44:29 +00:00
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
canReuse,
|
|
|
|
|
canDeactivate,
|
|
|
|
|
onActivate,
|
|
|
|
|
onReuse,
|
|
|
|
|
onDeactivate
|
|
|
|
|
} from './lifecycle_annotations_impl';
|
|
|
|
|
|
2015-08-05 19:02:38 +00:00
|
|
|
/**
|
2015-09-21 23:25:37 +00:00
|
|
|
* Defines route lifecycle hook `CanActivate`, which is called by the router to determine
|
2015-08-05 19:02:38 +00:00
|
|
|
* if a component can be instantiated as part of a navigation.
|
|
|
|
|
*
|
2015-09-21 23:25:37 +00:00
|
|
|
* The `CanActivate` hook is called with two {@link ComponentInstruction}s as parameters, the first
|
|
|
|
|
* representing
|
|
|
|
|
* the current route being navigated to, and the second parameter representing the previous route or
|
|
|
|
|
* `null`.
|
2015-08-05 19:02:38 +00:00
|
|
|
*
|
2015-09-21 23:25:37 +00:00
|
|
|
* Note that unlike other lifecycle hooks, this one uses an annotation rather than an interface.
|
|
|
|
|
* This is because the `CanActivate` function is called before the component is instantiated.
|
2015-08-05 19:02:38 +00:00
|
|
|
*
|
2015-09-21 23:25:37 +00:00
|
|
|
* If `CanActivate` returns or resolves to `false`, the navigation is cancelled.
|
|
|
|
|
* If `CanActivate` throws or rejects, the navigation is also cancelled.
|
|
|
|
|
* If `CanActivate` returns or resolves to `true`, navigation continues, the component is
|
|
|
|
|
* instantiated, and the {@link OnActivate} hook of that component is called if implemented.
|
2015-08-05 19:02:38 +00:00
|
|
|
*
|
2015-10-19 14:37:32 +00:00
|
|
|
* ### Example
|
2015-08-05 19:02:38 +00:00
|
|
|
* ```
|
2015-09-21 23:25:37 +00:00
|
|
|
* import {Component} from 'angular2/angular2';
|
|
|
|
|
* import {CanActivate} from 'angular2/router';
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
2015-10-11 14:41:19 +00:00
|
|
|
* selector: 'control-panel-cmp',
|
|
|
|
|
* template: '<div>Control Panel: ...</div>'
|
2015-09-21 23:25:37 +00:00
|
|
|
* })
|
2015-08-05 19:02:38 +00:00
|
|
|
* @CanActivate(() => checkIfUserIsLoggedIn())
|
|
|
|
|
* class ControlPanelCmp {
|
|
|
|
|
* // ...
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2015-08-04 22:08:59 +00:00
|
|
|
export var CanActivate:
|
2015-08-05 19:02:38 +00:00
|
|
|
(hook: (next: ComponentInstruction, prev: ComponentInstruction) => Promise<boolean>| boolean) =>
|
|
|
|
|
ClassDecorator = makeDecorator(CanActivateAnnotation);
|