X Tutup
Skip to content

Commit a9a552c

Browse files
committed
feat(router): lifecycle hooks
Closes #2640
1 parent f5f85bb commit a9a552c

12 files changed

+819
-102
lines changed

modules/angular2/src/router/instruction.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export class Instruction {
2424
// "capturedUrl" is the part of the URL captured by this instruction
2525
// "accumulatedUrl" is the part of the URL captured by this instruction and all children
2626
accumulatedUrl: string;
27-
2827
reuse: boolean = false;
2928
specificity: number;
3029

@@ -50,23 +49,4 @@ export class Instruction {
5049
}
5150
return this._params;
5251
}
53-
54-
hasChild(): boolean { return isPresent(this.child); }
55-
56-
/**
57-
* Takes a currently active instruction and sets a reuse flag on each of this instruction's
58-
* children
59-
*/
60-
reuseComponentsFrom(oldInstruction: Instruction): void {
61-
var nextInstruction = this;
62-
while (nextInstruction.reuse = shouldReuseComponent(nextInstruction, oldInstruction) &&
63-
isPresent(oldInstruction = oldInstruction.child) &&
64-
isPresent(nextInstruction = nextInstruction.child))
65-
;
66-
}
67-
}
68-
69-
function shouldReuseComponent(instr1: Instruction, instr2: Instruction): boolean {
70-
return instr1.component == instr2.component &&
71-
StringMapWrapper.equals(instr1.params(), instr2.params());
7252
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Instruction} from './instruction';
2+
import {global} from 'angular2/src/facade/lang';
3+
4+
// This is here only so that after TS transpilation the file is not empty.
5+
// TODO(rado): find a better way to fix this, or remove if likely culprit
6+
// https://github.com/systemjs/systemjs/issues/487 gets closed.
7+
var __ignore_me = global;
8+
9+
10+
/**
11+
* Defines route lifecycle method [onActivate]
12+
*/
13+
export interface OnActivate {
14+
onActivate(nextInstruction: Instruction, prevInstruction: Instruction): any;
15+
}
16+
17+
/**
18+
* Defines route lifecycle method [onReuse]
19+
*/
20+
export interface OnReuse {
21+
onReuse(nextInstruction: Instruction, prevInstruction: Instruction): any;
22+
}
23+
24+
/**
25+
* Defines route lifecycle method [onDeactivate]
26+
*/
27+
export interface OnDeactivate {
28+
onDeactivate(nextInstruction: Instruction, prevInstruction: Instruction): any;
29+
}
30+
31+
/**
32+
* Defines route lifecycle method [canReuse]
33+
*/
34+
export interface CanReuse {
35+
canReuse(nextInstruction: Instruction, prevInstruction: Instruction): any;
36+
}
37+
38+
/**
39+
* Defines route lifecycle method [canDeactivate]
40+
*/
41+
export interface CanDeactivate {
42+
canDeactivate(nextInstruction: Instruction, prevInstruction: Instruction): any;
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This indirection is needed for TS compilation path.
3+
* See comment in lifecycle_annotations.ts.
4+
*/
5+
6+
library angular2.router.lifecycle_annotations;
7+
8+
export "./lifecycle_annotations_impl.dart";
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* This indirection is needed to free up Component, etc symbols in the public API
3+
* to be used by the decorator versions of these annotations.
4+
*/
5+
6+
import {makeDecorator} from 'angular2/src/util/decorators';
7+
import {CanActivate as CanActivateAnnotation} from './lifecycle_annotations_impl';
8+
9+
export {
10+
canReuse,
11+
canDeactivate,
12+
onActivate,
13+
onReuse,
14+
onDeactivate
15+
} from './lifecycle_annotations_impl';
16+
17+
export var CanActivate = makeDecorator(CanActivateAnnotation);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {CONST, CONST_EXPR} from 'angular2/src/facade/lang';
2+
3+
@CONST()
4+
export class RouteLifecycleHook {
5+
constructor(public name: string) {}
6+
}
7+
8+
@CONST()
9+
export class CanActivate {
10+
constructor(public fn: Function) {}
11+
}
12+
13+
export const canReuse: RouteLifecycleHook = CONST_EXPR(new RouteLifecycleHook("canReuse"));
14+
export const canDeactivate: RouteLifecycleHook =
15+
CONST_EXPR(new RouteLifecycleHook("canDeactivate"));
16+
export const onActivate: RouteLifecycleHook = CONST_EXPR(new RouteLifecycleHook("onActivate"));
17+
export const onReuse: RouteLifecycleHook = CONST_EXPR(new RouteLifecycleHook("onReuse"));
18+
export const onDeactivate: RouteLifecycleHook = CONST_EXPR(new RouteLifecycleHook("onDeactivate"));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library angular.router.route_lifecycle_reflector;
2+
3+
import 'package:angular2/src/router/lifecycle_annotations_impl.dart';
4+
import 'package:angular2/src/router/interfaces.dart';
5+
import 'package:angular2/src/reflection/reflection.dart';
6+
7+
bool hasLifecycleHook(RouteLifecycleHook e, type) {
8+
if (type is! Type) return false;
9+
10+
final List interfaces = reflector.interfaces(type);
11+
var interface;
12+
13+
if (e == onActivate) {
14+
interface = OnActivate;
15+
} else if (e == onDeactivate) {
16+
interface = OnDeactivate;
17+
} else if (e == onReuse) {
18+
interface = OnReuse;
19+
} else if (e == canDeactivate) {
20+
interface = CanDeactivate;
21+
} else if (e == canReuse) {
22+
interface = CanReuse;
23+
}
24+
25+
return interfaces.contains(interface);
26+
}
27+
28+
Function getCanActivateHook(type) {
29+
final List annotations = reflector.annotations(type);
30+
31+
for (var annotation in annotations) {
32+
if (annotation is CanActivate) {
33+
return annotation.fn;
34+
}
35+
}
36+
37+
return null;
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Type, isPresent} from 'angular2/src/facade/lang';
2+
import {RouteLifecycleHook, CanActivate} from './lifecycle_annotations_impl';
3+
import {reflector} from 'angular2/src/reflection/reflection';
4+
5+
export function hasLifecycleHook(e: RouteLifecycleHook, type): boolean {
6+
if (!(type instanceof Type)) return false;
7+
return e.name in(<any>type).prototype;
8+
}
9+
10+
export function getCanActivateHook(type): Function {
11+
var annotations = reflector.annotations(type);
12+
for (let i = 0; i < annotations.length; i += 1) {
13+
let annotation = annotations[i];
14+
if (annotation instanceof CanActivate) {
15+
return annotation.fn;
16+
}
17+
}
18+
19+
return null;
20+
}

modules/angular2/src/router/route_registry.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ export class RouteRegistry {
126126
this.configFromComponent(componentType);
127127

128128
if (partialMatch.unmatchedUrl.length == 0) {
129-
return new Instruction(componentType, partialMatch.matchedUrl, recognizer);
129+
if (recognizer.terminal) {
130+
return new Instruction(componentType, partialMatch.matchedUrl, recognizer);
131+
} else {
132+
return null;
133+
}
130134
}
131135

132136
return this.recognize(partialMatch.unmatchedUrl, componentType)

0 commit comments

Comments
 (0)
X Tutup