refactor(lifecycle): prefix lifecycle methods with "ng"

BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.

To fix, just rename these methods:
 * onInit
 * onDestroy
 * doCheck
 * onChanges
 * afterContentInit
 * afterContentChecked
 * afterViewInit
 * afterViewChecked
 * _Router Hooks_
 * onActivate
 * onReuse
 * onDeactivate
 * canReuse
 * canDeactivate

To:
 * ngOnInit,
 * ngOnDestroy,
 * ngDoCheck,
 * ngOnChanges,
 * ngAfterContentInit,
 * ngAfterContentChecked,
 * ngAfterViewInit,
 * ngAfterViewChecked
 * _Router Hooks_
 * routerOnActivate
 * routerOnReuse
 * routerOnDeactivate
 * routerCanReuse
 * routerCanDeactivate

The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.

Closes #5036
This commit is contained in:
Jeff Cross
2015-11-16 17:04:36 -08:00
committed by vsavkin
parent 4215afc639
commit 604c8bbad5
63 changed files with 618 additions and 583 deletions
@@ -256,13 +256,13 @@ export function main() {
});
});
describe("onChanges", () => {
describe("ngOnChanges", () => {
it("should update dom values of all the directives", () => {
form.addControl(loginControlDir);
(<Control>formModel.find(["login"])).updateValue("new value");
form.onChanges({});
form.ngOnChanges({});
expect((<any>loginControlDir.valueAccessor).writtenValue).toEqual("new value");
});
@@ -271,7 +271,7 @@ export function main() {
var formValidator = (c) => ({"custom": true});
var f = new NgFormModel([formValidator], []);
f.form = formModel;
f.onChanges({"form": new SimpleChange(null, null)});
f.ngOnChanges({"form": new SimpleChange(null, null)});
expect(formModel.errors).toEqual({"custom": true});
});
@@ -279,7 +279,7 @@ export function main() {
it("should set up an async validator", fakeAsync(() => {
var f = new NgFormModel([], [asyncValidator("expected")]);
f.form = formModel;
f.onChanges({"form": new SimpleChange(null, null)});
f.ngOnChanges({"form": new SimpleChange(null, null)});
tick();
@@ -417,7 +417,7 @@ export function main() {
it("should reexport new control properties", () => {
var newControl = new Control(null);
controlDir.form = newControl;
controlDir.onChanges({"form": new SimpleChange(control, newControl)});
controlDir.ngOnChanges({"form": new SimpleChange(control, newControl)});
checkProperties(newControl);
});
@@ -426,7 +426,7 @@ export function main() {
expect(control.valid).toBe(true);
// this will add the required validator and recalculate the validity
controlDir.onChanges({"form": new SimpleChange(null, control)});
controlDir.ngOnChanges({"form": new SimpleChange(null, control)});
expect(control.valid).toBe(false);
});
@@ -455,7 +455,7 @@ export function main() {
it("should set up validator", fakeAsync(() => {
// this will add the required validator and recalculate the validity
ngModel.onChanges({});
ngModel.ngOnChanges({});
tick();
expect(ngModel.control.errors).toEqual({"required": true});
@@ -153,13 +153,13 @@ export function main() {
expect(c.value).toEqual("newValue");
});
it("should invoke onChanges if it is present", () => {
var onChanges;
c.registerOnChange((v) => onChanges = ["invoked", v]);
it("should invoke ngOnChanges if it is present", () => {
var ngOnChanges;
c.registerOnChange((v) => ngOnChanges = ["invoked", v]);
c.updateValue("newValue");
expect(onChanges).toEqual(["invoked", "newValue"]);
expect(ngOnChanges).toEqual(["invoked", "newValue"]);
});
it("should not invoke on change when explicitly specified", () => {
@@ -136,12 +136,12 @@ class DirectiveWithoutModuleId {
class ComponentWithEverything implements OnChanges,
OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit,
AfterViewChecked {
onChanges(changes: {[key: string]: SimpleChange}): void {}
onInit(): void {}
doCheck(): void {}
onDestroy(): void {}
afterContentInit(): void {}
afterContentChecked(): void {}
afterViewInit(): void {}
afterViewChecked(): void {}
ngOnChanges(changes: {[key: string]: SimpleChange}): void {}
ngOnInit(): void {}
ngDoCheck(): void {}
ngOnDestroy(): void {}
ngAfterContentInit(): void {}
ngAfterContentChecked(): void {}
ngAfterViewInit(): void {}
ngAfterViewChecked(): void {}
}
@@ -468,13 +468,13 @@ export function main() {
it('should notify the dispatcher after content children have checked', () => {
var val = _createChangeDetector('name', new Person('bob'));
val.changeDetector.detectChanges();
expect(val.dispatcher.afterContentCheckedCalled).toEqual(true);
expect(val.dispatcher.ngAfterContentCheckedCalled).toEqual(true);
});
it('should notify the dispatcher after view children have been checked', () => {
var val = _createChangeDetector('name', new Person('bob'));
val.changeDetector.detectChanges();
expect(val.dispatcher.afterViewCheckedCalled).toEqual(true);
expect(val.dispatcher.ngAfterViewCheckedCalled).toEqual(true);
});
describe('updating directives', () => {
@@ -498,7 +498,7 @@ export function main() {
});
describe('lifecycle', () => {
describe('onChanges', () => {
describe('ngOnChanges', () => {
it('should notify the directive when a group of records changes', () => {
var cd = _createWithoutHydrate('groupChanges').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1, directive2], []),
@@ -509,32 +509,32 @@ export function main() {
});
});
describe('doCheck', () => {
describe('ngDoCheck', () => {
it('should notify the directive when it is checked', () => {
var cd = _createWithoutHydrate('directiveDoCheck').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.detectChanges();
expect(directive1.doCheckCalled).toBe(true);
directive1.doCheckCalled = false;
expect(directive1.ngDoCheckCalled).toBe(true);
directive1.ngDoCheckCalled = false;
cd.detectChanges();
expect(directive1.doCheckCalled).toBe(true);
expect(directive1.ngDoCheckCalled).toBe(true);
});
it('should not call doCheck in detectNoChanges', () => {
it('should not call ngDoCheck in detectNoChanges', () => {
var cd = _createWithoutHydrate('directiveDoCheck').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.checkNoChanges();
expect(directive1.doCheckCalled).toBe(false);
expect(directive1.ngDoCheckCalled).toBe(false);
});
});
describe('onInit', () => {
describe('ngOnInit', () => {
it('should notify the directive after it has been checked the first time', () => {
var cd = _createWithoutHydrate('directiveOnInit').changeDetector;
@@ -543,51 +543,51 @@ export function main() {
cd.detectChanges();
expect(directive1.onInitCalled).toBe(true);
expect(directive1.ngOnInitCalled).toBe(true);
directive1.onInitCalled = false;
directive1.ngOnInitCalled = false;
cd.detectChanges();
expect(directive1.onInitCalled).toBe(false);
expect(directive1.ngOnInitCalled).toBe(false);
});
it('should not call onInit in detectNoChanges', () => {
it('should not call ngOnInit in detectNoChanges', () => {
var cd = _createWithoutHydrate('directiveOnInit').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.checkNoChanges();
expect(directive1.onInitCalled).toBe(false);
expect(directive1.ngOnInitCalled).toBe(false);
});
it('should not call onInit again if it throws', () => {
it('should not call ngOnInit again if it throws', () => {
var cd = _createWithoutHydrate('directiveOnInit').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive3], []), null);
var errored = false;
// First pass fails, but onInit should be called.
// First pass fails, but ngOnInit should be called.
try {
cd.detectChanges();
} catch (e) {
errored = true;
}
expect(errored).toBe(true);
expect(directive3.onInitCalled).toBe(true);
directive3.onInitCalled = false;
expect(directive3.ngOnInitCalled).toBe(true);
directive3.ngOnInitCalled = false;
// Second change detection also fails, but this time onInit should not be called.
// Second change detection also fails, but this time ngOnInit should not be called.
try {
cd.detectChanges();
} catch (e) {
throw new BaseException("Second detectChanges() should not have run detection.");
}
expect(directive3.onInitCalled).toBe(false);
expect(directive3.ngOnInitCalled).toBe(false);
});
});
describe('afterContentInit', () => {
describe('ngAfterContentInit', () => {
it('should be called after processing the content children', () => {
var cd = _createWithoutHydrate('emptyWithDirectiveRecords').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1, directive2], []),
@@ -595,38 +595,38 @@ export function main() {
cd.detectChanges();
expect(directive1.afterContentInitCalled).toBe(true);
expect(directive2.afterContentInitCalled).toBe(true);
expect(directive1.ngAfterContentInitCalled).toBe(true);
expect(directive2.ngAfterContentInitCalled).toBe(true);
// reset directives
directive1.afterContentInitCalled = false;
directive2.afterContentInitCalled = false;
directive1.ngAfterContentInitCalled = false;
directive2.ngAfterContentInitCalled = false;
// Verify that checking should not call them.
cd.checkNoChanges();
expect(directive1.afterContentInitCalled).toBe(false);
expect(directive2.afterContentInitCalled).toBe(false);
expect(directive1.ngAfterContentInitCalled).toBe(false);
expect(directive2.ngAfterContentInitCalled).toBe(false);
// re-verify that changes should not call them
cd.detectChanges();
expect(directive1.afterContentInitCalled).toBe(false);
expect(directive2.afterContentInitCalled).toBe(false);
expect(directive1.ngAfterContentInitCalled).toBe(false);
expect(directive2.ngAfterContentInitCalled).toBe(false);
});
it('should not be called when afterContentInit is false', () => {
it('should not be called when ngAfterContentInit is false', () => {
var cd = _createWithoutHydrate('noCallbacks').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.detectChanges();
expect(directive1.afterContentInitCalled).toEqual(false);
expect(directive1.ngAfterContentInitCalled).toEqual(false);
});
});
describe('afterContentChecked', () => {
describe('ngAfterContentChecked', () => {
it('should be called after processing all the children', () => {
var cd = _createWithoutHydrate('emptyWithDirectiveRecords').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1, directive2], []),
@@ -634,50 +634,50 @@ export function main() {
cd.detectChanges();
expect(directive1.afterContentCheckedCalled).toBe(true);
expect(directive2.afterContentCheckedCalled).toBe(true);
expect(directive1.ngAfterContentCheckedCalled).toBe(true);
expect(directive2.ngAfterContentCheckedCalled).toBe(true);
// reset directives
directive1.afterContentCheckedCalled = false;
directive2.afterContentCheckedCalled = false;
directive1.ngAfterContentCheckedCalled = false;
directive2.ngAfterContentCheckedCalled = false;
// Verify that checking should not call them.
cd.checkNoChanges();
expect(directive1.afterContentCheckedCalled).toBe(false);
expect(directive2.afterContentCheckedCalled).toBe(false);
expect(directive1.ngAfterContentCheckedCalled).toBe(false);
expect(directive2.ngAfterContentCheckedCalled).toBe(false);
// re-verify that changes are still detected
cd.detectChanges();
expect(directive1.afterContentCheckedCalled).toBe(true);
expect(directive2.afterContentCheckedCalled).toBe(true);
expect(directive1.ngAfterContentCheckedCalled).toBe(true);
expect(directive2.ngAfterContentCheckedCalled).toBe(true);
});
it('should not be called when afterContentChecked is false', () => {
it('should not be called when ngAfterContentChecked is false', () => {
var cd = _createWithoutHydrate('noCallbacks').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.detectChanges();
expect(directive1.afterContentCheckedCalled).toEqual(false);
expect(directive1.ngAfterContentCheckedCalled).toEqual(false);
});
it('should be called in reverse order so the child is always notified before the parent',
() => {
var cd = _createWithoutHydrate('emptyWithDirectiveRecords').changeDetector;
var onChangesDoneCalls = [];
var ngOnChangesDoneCalls = [];
var td1;
td1 = new TestDirective(() => onChangesDoneCalls.push(td1));
td1 = new TestDirective(() => ngOnChangesDoneCalls.push(td1));
var td2;
td2 = new TestDirective(() => onChangesDoneCalls.push(td2));
td2 = new TestDirective(() => ngOnChangesDoneCalls.push(td2));
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([td1, td2], []), null);
cd.detectChanges();
expect(onChangesDoneCalls).toEqual([td2, td1]);
expect(ngOnChangesDoneCalls).toEqual([td2, td1]);
});
it('should be called before processing view children', () => {
@@ -705,7 +705,7 @@ export function main() {
});
describe('afterViewInit', () => {
describe('ngAfterViewInit', () => {
it('should be called after processing the view children', () => {
var cd = _createWithoutHydrate('emptyWithDirectiveRecords').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1, directive2], []),
@@ -713,39 +713,39 @@ export function main() {
cd.detectChanges();
expect(directive1.afterViewInitCalled).toBe(true);
expect(directive2.afterViewInitCalled).toBe(true);
expect(directive1.ngAfterViewInitCalled).toBe(true);
expect(directive2.ngAfterViewInitCalled).toBe(true);
// reset directives
directive1.afterViewInitCalled = false;
directive2.afterViewInitCalled = false;
directive1.ngAfterViewInitCalled = false;
directive2.ngAfterViewInitCalled = false;
// Verify that checking should not call them.
cd.checkNoChanges();
expect(directive1.afterViewInitCalled).toBe(false);
expect(directive2.afterViewInitCalled).toBe(false);
expect(directive1.ngAfterViewInitCalled).toBe(false);
expect(directive2.ngAfterViewInitCalled).toBe(false);
// re-verify that changes should not call them
cd.detectChanges();
expect(directive1.afterViewInitCalled).toBe(false);
expect(directive2.afterViewInitCalled).toBe(false);
expect(directive1.ngAfterViewInitCalled).toBe(false);
expect(directive2.ngAfterViewInitCalled).toBe(false);
});
it('should not be called when afterViewInit is false', () => {
it('should not be called when ngAfterViewInit is false', () => {
var cd = _createWithoutHydrate('noCallbacks').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.detectChanges();
expect(directive1.afterViewInitCalled).toEqual(false);
expect(directive1.ngAfterViewInitCalled).toEqual(false);
});
});
describe('afterViewChecked', () => {
describe('ngAfterViewChecked', () => {
it('should be called after processing the view children', () => {
var cd = _createWithoutHydrate('emptyWithDirectiveRecords').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1, directive2], []),
@@ -753,50 +753,50 @@ export function main() {
cd.detectChanges();
expect(directive1.afterViewCheckedCalled).toBe(true);
expect(directive2.afterViewCheckedCalled).toBe(true);
expect(directive1.ngAfterViewCheckedCalled).toBe(true);
expect(directive2.ngAfterViewCheckedCalled).toBe(true);
// reset directives
directive1.afterViewCheckedCalled = false;
directive2.afterViewCheckedCalled = false;
directive1.ngAfterViewCheckedCalled = false;
directive2.ngAfterViewCheckedCalled = false;
// Verify that checking should not call them.
cd.checkNoChanges();
expect(directive1.afterViewCheckedCalled).toBe(false);
expect(directive2.afterViewCheckedCalled).toBe(false);
expect(directive1.ngAfterViewCheckedCalled).toBe(false);
expect(directive2.ngAfterViewCheckedCalled).toBe(false);
// re-verify that changes should call them
cd.detectChanges();
expect(directive1.afterViewCheckedCalled).toBe(true);
expect(directive2.afterViewCheckedCalled).toBe(true);
expect(directive1.ngAfterViewCheckedCalled).toBe(true);
expect(directive2.ngAfterViewCheckedCalled).toBe(true);
});
it('should not be called when afterViewChecked is false', () => {
it('should not be called when ngAfterViewChecked is false', () => {
var cd = _createWithoutHydrate('noCallbacks').changeDetector;
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([directive1], []), null);
cd.detectChanges();
expect(directive1.afterViewCheckedCalled).toEqual(false);
expect(directive1.ngAfterViewCheckedCalled).toEqual(false);
});
it('should be called in reverse order so the child is always notified before the parent',
() => {
var cd = _createWithoutHydrate('emptyWithDirectiveRecords').changeDetector;
var onChangesDoneCalls = [];
var ngOnChangesDoneCalls = [];
var td1;
td1 = new TestDirective(null, () => onChangesDoneCalls.push(td1));
td1 = new TestDirective(null, () => ngOnChangesDoneCalls.push(td1));
var td2;
td2 = new TestDirective(null, () => onChangesDoneCalls.push(td2));
td2 = new TestDirective(null, () => ngOnChangesDoneCalls.push(td2));
cd.hydrate(_DEFAULT_CONTEXT, null, new FakeDirectives([td1, td2], []), null);
cd.detectChanges();
expect(onChangesDoneCalls).toEqual([td2, td1]);
expect(ngOnChangesDoneCalls).toEqual([td2, td1]);
});
it('should be called after processing view children', () => {
@@ -1408,51 +1408,51 @@ class TestDirective {
a;
b;
changes;
doCheckCalled = false;
onInitCalled = false;
ngDoCheckCalled = false;
ngOnInitCalled = false;
afterContentInitCalled = false;
afterContentCheckedCalled = false;
ngAfterContentInitCalled = false;
ngAfterContentCheckedCalled = false;
afterViewInitCalled = false;
afterViewCheckedCalled = false;
ngAfterViewInitCalled = false;
ngAfterViewCheckedCalled = false;
event;
constructor(public afterContentCheckedSpy = null, public afterViewCheckedSpy = null,
constructor(public ngAfterContentCheckedSpy = null, public ngAfterViewCheckedSpy = null,
public throwOnInit = false) {}
onEvent(event) { this.event = event; }
doCheck() { this.doCheckCalled = true; }
ngDoCheck() { this.ngDoCheckCalled = true; }
onInit() {
this.onInitCalled = true;
ngOnInit() {
this.ngOnInitCalled = true;
if (this.throwOnInit) {
throw "simulated onInit failure";
throw "simulated ngOnInit failure";
}
}
onChanges(changes) {
ngOnChanges(changes) {
var r = {};
StringMapWrapper.forEach(changes, (c, key) => r[key] = c.currentValue);
this.changes = r;
}
afterContentInit() { this.afterContentInitCalled = true; }
ngAfterContentInit() { this.ngAfterContentInitCalled = true; }
afterContentChecked() {
this.afterContentCheckedCalled = true;
if (isPresent(this.afterContentCheckedSpy)) {
this.afterContentCheckedSpy();
ngAfterContentChecked() {
this.ngAfterContentCheckedCalled = true;
if (isPresent(this.ngAfterContentCheckedSpy)) {
this.ngAfterContentCheckedSpy();
}
}
afterViewInit() { this.afterViewInitCalled = true; }
ngAfterViewInit() { this.ngAfterViewInitCalled = true; }
afterViewChecked() {
this.afterViewCheckedCalled = true;
if (isPresent(this.afterViewCheckedSpy)) {
this.afterViewCheckedSpy();
ngAfterViewChecked() {
this.ngAfterViewCheckedCalled = true;
if (isPresent(this.ngAfterViewCheckedSpy)) {
this.ngAfterViewCheckedSpy();
}
}
}
@@ -1531,8 +1531,8 @@ class TestDispatcher implements ChangeDispatcher {
log: string[];
debugLog: string[];
loggedValues: any[];
afterContentCheckedCalled: boolean = false;
afterViewCheckedCalled: boolean = false;
ngAfterContentCheckedCalled: boolean = false;
ngAfterViewCheckedCalled: boolean = false;
constructor() { this.clear(); }
@@ -1540,7 +1540,7 @@ class TestDispatcher implements ChangeDispatcher {
this.log = [];
this.debugLog = [];
this.loggedValues = [];
this.afterContentCheckedCalled = true;
this.ngAfterContentCheckedCalled = true;
}
notifyOnBinding(target, value) {
@@ -1550,8 +1550,8 @@ class TestDispatcher implements ChangeDispatcher {
logBindingUpdate(target, value) { this.debugLog.push(`${target.name}=${this._asString(value)}`); }
notifyAfterContentChecked() { this.afterContentCheckedCalled = true; }
notifyAfterViewChecked() { this.afterViewCheckedCalled = true; }
notifyAfterContentChecked() { this.ngAfterContentCheckedCalled = true; }
notifyAfterViewChecked() { this.ngAfterViewCheckedCalled = true; }
getDebugContext(a, b) { return null; }
@@ -84,8 +84,8 @@ export function main() {
it("should not coalesce directive lifecycle records", () => {
var rs = coalesce([
r("doCheck", [], 0, 1, {mode: RecordType.DirectiveLifecycle}),
r("doCheck", [], 0, 1, {mode: RecordType.DirectiveLifecycle})
r("ngDoCheck", [], 0, 1, {mode: RecordType.DirectiveLifecycle}),
r("ngDoCheck", [], 0, 1, {mode: RecordType.DirectiveLifecycle})
]);
expect(rs.length).toEqual(2);
@@ -27,7 +27,7 @@ import {Directive, Component, View, ViewMetadata} from 'angular2/src/core/metada
export function main() {
describe('directive lifecycle integration spec', () => {
it('should invoke lifecycle methods onChanges > onInit > doCheck > afterContentChecked',
it('should invoke lifecycle methods ngOnChanges > ngOnInit > ngDoCheck > ngAfterContentChecked',
inject([TestComponentBuilder, Log, AsyncTestCompleter], (tcb: TestComponentBuilder, log: Log,
async) => {
tcb.overrideView(
@@ -40,14 +40,15 @@ export function main() {
expect(log.result())
.toEqual(
"onChanges; onInit; doCheck; afterContentInit; afterContentChecked; child_doCheck; " +
"afterViewInit; afterViewChecked");
"ngOnChanges; ngOnInit; ngDoCheck; ngAfterContentInit; ngAfterContentChecked; child_ngDoCheck; " +
"ngAfterViewInit; ngAfterViewChecked");
log.clear();
tc.detectChanges();
expect(log.result())
.toEqual("doCheck; afterContentChecked; child_doCheck; afterViewChecked");
.toEqual(
"ngDoCheck; ngAfterContentChecked; child_ngDoCheck; ngAfterViewChecked");
async.done();
});
@@ -59,7 +60,7 @@ export function main() {
@Directive({selector: '[lifecycle-dir]'})
class LifecycleDir implements DoCheck {
constructor(private _log: Log) {}
doCheck() { this._log.add("child_doCheck"); }
ngDoCheck() { this._log.add("child_ngDoCheck"); }
}
@Component({selector: "[lifecycle]", inputs: ['field']})
@@ -69,19 +70,19 @@ class LifecycleCmp implements OnChanges, OnInit, DoCheck, AfterContentInit, Afte
field;
constructor(private _log: Log) {}
onChanges(_) { this._log.add("onChanges"); }
ngOnChanges(_) { this._log.add("ngOnChanges"); }
onInit() { this._log.add("onInit"); }
ngOnInit() { this._log.add("ngOnInit"); }
doCheck() { this._log.add("doCheck"); }
ngDoCheck() { this._log.add("ngDoCheck"); }
afterContentInit() { this._log.add("afterContentInit"); }
ngAfterContentInit() { this._log.add("ngAfterContentInit"); }
afterContentChecked() { this._log.add("afterContentChecked"); }
ngAfterContentChecked() { this._log.add("ngAfterContentChecked"); }
afterViewInit() { this._log.add("afterViewInit"); }
ngAfterViewInit() { this._log.add("ngAfterViewInit"); }
afterViewChecked() { this._log.add("afterViewChecked"); }
ngAfterViewChecked() { this._log.add("ngAfterViewChecked"); }
}
@Component({selector: 'my-comp'})
@@ -7,8 +7,8 @@ import 'package:angular2/src/core/linker/interfaces.dart';
main() {
describe('Create DirectiveMetadata', () {
describe('lifecycle', () {
describe("onChanges", () {
it("should be true when the directive has the onChanges method", () {
describe("ngOnChanges", () {
it("should be true when the directive has the ngOnChanges method", () {
expect(hasLifecycleHook(
LifecycleHooks.OnChanges, DirectiveImplementingOnChanges))
.toBe(true);
@@ -20,8 +20,8 @@ main() {
});
});
describe("onDestroy", () {
it("should be true when the directive has the onDestroy method", () {
describe("ngOnDestroy", () {
it("should be true when the directive has the ngOnDestroy method", () {
expect(hasLifecycleHook(
LifecycleHooks.OnDestroy, DirectiveImplementingOnDestroy))
.toBe(true);
@@ -33,8 +33,8 @@ main() {
});
});
describe("onInit", () {
it("should be true when the directive has the onInit method", () {
describe("ngOnInit", () {
it("should be true when the directive has the ngOnInit method", () {
expect(hasLifecycleHook(
LifecycleHooks.OnInit, DirectiveImplementingOnInit)).toBe(true);
});
@@ -45,8 +45,8 @@ main() {
});
});
describe("doCheck", () {
it("should be true when the directive has the doCheck method", () {
describe("ngDoCheck", () {
it("should be true when the directive has the ngDoCheck method", () {
expect(hasLifecycleHook(
LifecycleHooks.DoCheck, DirectiveImplementingOnCheck)).toBe(true);
});
@@ -57,8 +57,8 @@ main() {
});
});
describe("afterContentInit", () {
it("should be true when the directive has the afterContentInit method",
describe("ngAfterContentInit", () {
it("should be true when the directive has the ngAfterContentInit method",
() {
expect(hasLifecycleHook(LifecycleHooks.AfterContentInit,
DirectiveImplementingAfterContentInit)).toBe(true);
@@ -70,8 +70,8 @@ main() {
});
});
describe("afterContentChecked", () {
it("should be true when the directive has the afterContentChecked method",
describe("ngAfterContentChecked", () {
it("should be true when the directive has the ngAfterContentChecked method",
() {
expect(hasLifecycleHook(LifecycleHooks.AfterContentChecked,
DirectiveImplementingAfterContentChecked)).toBe(true);
@@ -84,8 +84,8 @@ main() {
});
});
describe("afterViewInit", () {
it("should be true when the directive has the afterViewInit method",
describe("ngAfterViewInit", () {
it("should be true when the directive has the ngAfterViewInit method",
() {
expect(hasLifecycleHook(LifecycleHooks.AfterViewInit,
DirectiveImplementingAfterViewInit)).toBe(true);
@@ -97,8 +97,8 @@ main() {
});
});
describe("afterViewChecked", () {
it("should be true when the directive has the afterViewChecked method",
describe("ngAfterViewChecked", () {
it("should be true when the directive has the ngAfterViewChecked method",
() {
expect(hasLifecycleHook(LifecycleHooks.AfterViewChecked,
DirectiveImplementingAfterViewChecked)).toBe(true);
@@ -116,33 +116,33 @@ main() {
class DirectiveNoHooks {}
class DirectiveImplementingOnChanges implements OnChanges {
onChanges(_) {}
ngOnChanges(_) {}
}
class DirectiveImplementingOnCheck implements DoCheck {
doCheck() {}
ngDoCheck() {}
}
class DirectiveImplementingOnInit implements OnInit {
onInit() {}
ngOnInit() {}
}
class DirectiveImplementingOnDestroy implements OnDestroy {
onDestroy() {}
ngOnDestroy() {}
}
class DirectiveImplementingAfterContentInit implements AfterContentInit {
afterContentInit() {}
ngAfterContentInit() {}
}
class DirectiveImplementingAfterContentChecked implements AfterContentChecked {
afterContentChecked() {}
ngAfterContentChecked() {}
}
class DirectiveImplementingAfterViewInit implements AfterViewInit {
afterViewInit() {}
ngAfterViewInit() {}
}
class DirectiveImplementingAfterViewChecked implements AfterViewChecked {
afterViewChecked() {}
ngAfterViewChecked() {}
}
@@ -20,8 +20,8 @@ export function main() {
describe('Create DirectiveMetadata', () => {
describe('lifecycle', () => {
describe("onChanges", () => {
it("should be true when the directive has the onChanges method", () => {
describe("ngOnChanges", () => {
it("should be true when the directive has the ngOnChanges method", () => {
expect(hasLifecycleHook(LifecycleHooks.OnChanges, DirectiveWithOnChangesMethod))
.toBe(true);
});
@@ -31,8 +31,8 @@ export function main() {
});
});
describe("onDestroy", () => {
it("should be true when the directive has the onDestroy method", () => {
describe("ngOnDestroy", () => {
it("should be true when the directive has the ngOnDestroy method", () => {
expect(hasLifecycleHook(LifecycleHooks.OnDestroy, DirectiveWithOnDestroyMethod))
.toBe(true);
});
@@ -42,8 +42,8 @@ export function main() {
});
});
describe("onInit", () => {
it("should be true when the directive has the onInit method", () => {
describe("ngOnInit", () => {
it("should be true when the directive has the ngOnInit method", () => {
expect(hasLifecycleHook(LifecycleHooks.OnInit, DirectiveWithOnInitMethod)).toBe(true);
});
@@ -52,8 +52,8 @@ export function main() {
});
});
describe("doCheck", () => {
it("should be true when the directive has the doCheck method", () => {
describe("ngDoCheck", () => {
it("should be true when the directive has the ngDoCheck method", () => {
expect(hasLifecycleHook(LifecycleHooks.DoCheck, DirectiveWithOnCheckMethod)).toBe(true);
});
@@ -62,8 +62,8 @@ export function main() {
});
});
describe("afterContentInit", () => {
it("should be true when the directive has the afterContentInit method", () => {
describe("ngAfterContentInit", () => {
it("should be true when the directive has the ngAfterContentInit method", () => {
expect(hasLifecycleHook(LifecycleHooks.AfterContentInit,
DirectiveWithAfterContentInitMethod))
.toBe(true);
@@ -74,8 +74,8 @@ export function main() {
});
});
describe("afterContentChecked", () => {
it("should be true when the directive has the afterContentChecked method", () => {
describe("ngAfterContentChecked", () => {
it("should be true when the directive has the ngAfterContentChecked method", () => {
expect(hasLifecycleHook(LifecycleHooks.AfterContentChecked,
DirectiveWithAfterContentCheckedMethod))
.toBe(true);
@@ -88,8 +88,8 @@ export function main() {
});
describe("afterViewInit", () => {
it("should be true when the directive has the afterViewInit method", () => {
describe("ngAfterViewInit", () => {
it("should be true when the directive has the ngAfterViewInit method", () => {
expect(hasLifecycleHook(LifecycleHooks.AfterViewInit, DirectiveWithAfterViewInitMethod))
.toBe(true);
});
@@ -99,8 +99,8 @@ export function main() {
});
});
describe("afterViewChecked", () => {
it("should be true when the directive has the afterViewChecked method", () => {
describe("ngAfterViewChecked", () => {
it("should be true when the directive has the ngAfterViewChecked method", () => {
expect(hasLifecycleHook(LifecycleHooks.AfterViewChecked,
DirectiveWithAfterViewCheckedMethod))
.toBe(true);
@@ -117,33 +117,33 @@ export function main() {
class DirectiveNoHooks {}
class DirectiveWithOnChangesMethod {
onChanges(_) {}
ngOnChanges(_) {}
}
class DirectiveWithOnInitMethod {
onInit() {}
ngOnInit() {}
}
class DirectiveWithOnCheckMethod {
doCheck() {}
ngDoCheck() {}
}
class DirectiveWithOnDestroyMethod {
onDestroy() {}
ngOnDestroy() {}
}
class DirectiveWithAfterContentInitMethod {
afterContentInit() {}
ngAfterContentInit() {}
}
class DirectiveWithAfterContentCheckedMethod {
afterContentChecked() {}
ngAfterContentChecked() {}
}
class DirectiveWithAfterViewInitMethod {
afterViewInit() {}
ngAfterViewInit() {}
}
class DirectiveWithAfterViewCheckedMethod {
afterViewChecked() {}
ngAfterViewChecked() {}
}
@@ -283,7 +283,7 @@ class DynamicallyCreatedCmp implements OnDestroy {
this.dynamicallyCreatedComponentService = a;
}
onDestroy() { this.destroyed = true; }
ngOnDestroy() { this.destroyed = true; }
}
@Component({selector: 'dummy'})
@@ -218,11 +218,11 @@ class B_Needs_A {
}
class DirectiveWithDestroy implements OnDestroy {
onDestroyCounter: number;
ngOnDestroyCounter: number;
constructor() { this.onDestroyCounter = 0; }
constructor() { this.ngOnDestroyCounter = 0; }
onDestroy() { this.onDestroyCounter++; }
ngOnDestroy() { this.ngOnDestroyCounter++; }
}
export function main() {
@@ -823,14 +823,14 @@ export function main() {
});
describe("lifecycle", () => {
it("should call onDestroy on directives subscribed to this event", () => {
it("should call ngOnDestroy on directives subscribed to this event", () => {
var inj = injector(ListWrapper.concat(
[DirectiveProvider.createFromType(DirectiveWithDestroy,
new DirectiveMetadata())],
extraProviders));
var destroy = inj.get(DirectiveWithDestroy);
inj.dehydrate();
expect(destroy.onDestroyCounter).toBe(1);
expect(destroy.ngOnDestroyCounter).toBe(1);
});
it("should work with services", () => {
@@ -956,7 +956,7 @@ export function main() {
false, preBuildObjects);
addInj(dummyView, inj);
inj.afterContentChecked();
inj.ngAfterContentChecked();
expectDirectives(inj.get(NeedsQuery).query, CountingDirective, [0]);
});
@@ -969,7 +969,7 @@ export function main() {
false, preBuiltObjects);
addInj(dummyView, inj);
inj.afterContentChecked();
inj.ngAfterContentChecked();
expect(inj.get(NeedsTemplateRefQuery).query.first).toBe(preBuiltObjects.templateRef);
});
@@ -985,7 +985,7 @@ export function main() {
false, preBuildObjects, null, dirVariableBindings);
addInj(dummyView, inj);
inj.afterContentChecked();
inj.ngAfterContentChecked();
expect(inj.get(NeedsQueryByVarBindings).query.first).toBe(preBuildObjects.elementRef);
});
@@ -1003,7 +1003,7 @@ export function main() {
false, preBuildObjects, null, dirVariableBindings);
addInj(dummyView, inj);
inj.afterContentChecked();
inj.ngAfterContentChecked();
// NeedsQueryByVarBindings queries "one,two", so SimpleDirective should be before NeedsDirective
expect(inj.get(NeedsQueryByVarBindings).query.first).toBeAnInstanceOf(SimpleDirective);
@@ -1022,7 +1022,7 @@ export function main() {
addInj(dummyView, parent);
addInj(dummyView, child);
parent.afterContentChecked();
parent.ngAfterContentChecked();
expectDirectives(parent.get(NeedsQuery).query, CountingDirective, [0, 1]);
});
@@ -275,7 +275,7 @@ class OnChangeComponent implements OnChanges {
String prop;
@override
void onChanges(Map changes) {
void ngOnChanges(Map changes) {
this.changes = changes;
}
}
@@ -305,5 +305,5 @@ class DirectiveLoggingChecks implements DoCheck {
DirectiveLoggingChecks(this.log);
doCheck() => log.add("check");
ngDoCheck() => log.add("check");
}
@@ -680,7 +680,7 @@ class NeedsContentChildren implements AfterContentInit {
@ContentChildren(TextDirective) textDirChildren: QueryList<TextDirective>;
numberOfChildrenAfterContentInit: number;
afterContentInit() { this.numberOfChildrenAfterContentInit = this.textDirChildren.length; }
ngAfterContentInit() { this.numberOfChildrenAfterContentInit = this.textDirChildren.length; }
}
@Component({selector: 'needs-view-children'})
@@ -689,7 +689,7 @@ class NeedsViewChildren implements AfterViewInit {
@ViewChildren(TextDirective) textDirChildren: QueryList<TextDirective>;
numberOfChildrenAfterViewInit: number;
afterViewInit() { this.numberOfChildrenAfterViewInit = this.textDirChildren.length; }
ngAfterViewInit() { this.numberOfChildrenAfterViewInit = this.textDirChildren.length; }
}
@Component({selector: 'needs-content-child'})
@@ -706,9 +706,9 @@ class NeedsContentChild implements AfterContentInit, AfterContentChecked {
get child() { return this._child; }
log = [];
afterContentInit() { this.log.push(["init", isPresent(this.child) ? this.child.text : null]); }
ngAfterContentInit() { this.log.push(["init", isPresent(this.child) ? this.child.text : null]); }
afterContentChecked() {
ngAfterContentChecked() {
this.log.push(["check", isPresent(this.child) ? this.child.text : null]);
}
}
@@ -734,9 +734,9 @@ class NeedsViewChild implements AfterViewInit,
get child() { return this._child; }
log = [];
afterViewInit() { this.log.push(["init", isPresent(this.child) ? this.child.text : null]); }
ngAfterViewInit() { this.log.push(["init", isPresent(this.child) ? this.child.text : null]); }
afterViewChecked() { this.log.push(["check", isPresent(this.child) ? this.child.text : null]); }
ngAfterViewChecked() { this.log.push(["check", isPresent(this.child) ? this.child.text : null]); }
}
@@ -76,7 +76,7 @@ class HelloOnDestroyTickCmp implements OnDestroy {
appRef: ApplicationRef;
constructor(@Inject(ApplicationRef) appRef) { this.appRef = appRef; }
onDestroy(): void { this.appRef.tick(); }
ngOnDestroy(): void { this.appRef.tick(); }
}
class _ArrayLogger {
+11 -11
View File
@@ -858,9 +858,9 @@ var NG_ALL = [
'NG_VALUE_ACCESSOR',
'NG_ASYNC_VALIDATORS',
'NgClass',
'NgClass.doCheck()',
'NgClass.ngDoCheck()',
'NgClass.initialClasses=',
'NgClass.onDestroy()',
'NgClass.ngOnDestroy()',
'NgClass.rawClass=',
'NgControl',
'NgControl.control',
@@ -885,8 +885,8 @@ var NG_ALL = [
'NgControlGroup.formDirective',
'NgControlGroup.name',
'NgControlGroup.name=',
'NgControlGroup.onDestroy()',
'NgControlGroup.onInit()',
'NgControlGroup.ngOnDestroy()',
'NgControlGroup.ngOnInit()',
'NgControlGroup.path',
'NgControlGroup.pristine',
'NgControlGroup.touched',
@@ -911,8 +911,8 @@ var NG_ALL = [
'NgControlName.model=',
'NgControlName.name',
'NgControlName.name=',
'NgControlName.onChanges()',
'NgControlName.onDestroy()',
'NgControlName.ngOnChanges()',
'NgControlName.ngOnDestroy()',
'NgControlName.path',
'NgControlName.pristine',
'NgControlName.touched',
@@ -929,7 +929,7 @@ var NG_ALL = [
'NgControlName.viewModel=',
'NgControlName.viewToModelUpdate()',
'NgFor',
'NgFor.doCheck()',
'NgFor.ngDoCheck()',
'NgFor.ngForOf=',
'NgFor.ngForTemplate=',
'NgForm',
@@ -968,7 +968,7 @@ var NG_ALL = [
'NgFormControl.model=',
'NgFormControl.name',
'NgFormControl.name=',
'NgFormControl.onChanges()',
'NgFormControl.ngOnChanges()',
'NgFormControl.path',
'NgFormControl.pristine',
'NgFormControl.touched',
@@ -1001,7 +1001,7 @@ var NG_ALL = [
'NgFormModel.name=',
'NgFormModel.ngSubmit',
'NgFormModel.ngSubmit=',
'NgFormModel.onChanges()',
'NgFormModel.ngOnChanges()',
'NgFormModel.onSubmit()',
'NgFormModel.path',
'NgFormModel.pristine',
@@ -1022,7 +1022,7 @@ var NG_ALL = [
'NgModel.model=',
'NgModel.name',
'NgModel.name=',
'NgModel.onChanges()',
'NgModel.ngOnChanges()',
'NgModel.path',
'NgModel.pristine',
'NgModel.touched',
@@ -1040,7 +1040,7 @@ var NG_ALL = [
'NgModel.viewToModelUpdate()',
'NgSelectOption',
'NgStyle',
'NgStyle.doCheck()',
'NgStyle.ngDoCheck()',
'NgStyle.rawStyle=',
'NgSwitch',
'NgSwitch.ngSwitch=',
@@ -274,7 +274,7 @@ class AppWithViewChildren implements AfterViewInit {
constructor(public router: Router, public location: LocationStrategy) {}
afterViewInit() { this.helloCmp.message = 'Ahoy'; }
ngAfterViewInit() { this.helloCmp.message = 'Ahoy'; }
}
@Component({
@@ -69,7 +69,7 @@ export function main() {
eventBus = new EventEmitter();
}));
it('should call the onActivate hook', inject([AsyncTestCompleter], (async) => {
it('should call the routerOnActivate hook', inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
@@ -82,7 +82,7 @@ export function main() {
});
}));
it('should wait for a parent component\'s onActivate hook to resolve before calling its child\'s',
it('should wait for a parent component\'s routerOnActivate hook to resolve before calling its child\'s',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
@@ -106,7 +106,7 @@ export function main() {
});
}));
it('should call the onDeactivate hook', inject([AsyncTestCompleter], (async) => {
it('should call the routerOnDeactivate hook', inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
@@ -120,7 +120,7 @@ export function main() {
});
}));
it('should wait for a child component\'s onDeactivate hook to resolve before calling its parent\'s',
it('should wait for a child component\'s routerOnDeactivate hook to resolve before calling its parent\'s',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
@@ -146,7 +146,7 @@ export function main() {
});
}));
it('should reuse a component when the canReuse hook returns true',
it('should reuse a component when the routerCanReuse hook returns true',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
@@ -169,7 +169,7 @@ export function main() {
}));
it('should not reuse a component when the canReuse hook returns false',
it('should not reuse a component when the routerCanReuse hook returns false',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
@@ -192,34 +192,35 @@ export function main() {
}));
it('should navigate when canActivate returns true', inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
.then((_) => {
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('canActivate')) {
completer.resolve(true);
}
});
rtr.navigateByUrl('/can-activate/a')
.then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('canActivate {A}');
expect(log).toEqual(['canActivate: null -> /can-activate']);
async.done();
});
});
}));
it('should not navigate when canActivate returns false',
it('should navigate when routerCanActivate returns true',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
.then((_) => {
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('canActivate')) {
if (ev.startsWith('routerCanActivate')) {
completer.resolve(true);
}
});
rtr.navigateByUrl('/can-activate/a')
.then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('routerCanActivate {A}');
expect(log).toEqual(['routerCanActivate: null -> /can-activate']);
async.done();
});
});
}));
it('should not navigate when routerCanActivate returns false',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
.then((_) => {
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('routerCanActivate')) {
completer.resolve(false);
}
});
@@ -227,13 +228,13 @@ export function main() {
.then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('');
expect(log).toEqual(['canActivate: null -> /can-activate']);
expect(log).toEqual(['routerCanActivate: null -> /can-activate']);
async.done();
});
});
}));
it('should navigate away when canDeactivate returns true',
it('should navigate away when routerCanDeactivate returns true',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
@@ -241,11 +242,11 @@ export function main() {
.then((_) => rtr.navigateByUrl('/can-deactivate/a'))
.then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('canDeactivate {A}');
expect(fixture.debugElement.nativeElement).toHaveText('routerCanDeactivate {A}');
expect(log).toEqual([]);
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('canDeactivate')) {
if (ev.startsWith('routerCanDeactivate')) {
completer.resolve(true);
}
});
@@ -253,13 +254,13 @@ export function main() {
rtr.navigateByUrl('/a').then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('A');
expect(log).toEqual(['canDeactivate: /can-deactivate -> /a']);
expect(log).toEqual(['routerCanDeactivate: /can-deactivate -> /a']);
async.done();
});
});
}));
it('should not navigate away when canDeactivate returns false',
it('should not navigate away when routerCanDeactivate returns false',
inject([AsyncTestCompleter], (async) => {
compile(tcb)
.then((rtc) => {fixture = rtc})
@@ -267,19 +268,19 @@ export function main() {
.then((_) => rtr.navigateByUrl('/can-deactivate/a'))
.then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('canDeactivate {A}');
expect(fixture.debugElement.nativeElement).toHaveText('routerCanDeactivate {A}');
expect(log).toEqual([]);
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('canDeactivate')) {
if (ev.startsWith('routerCanDeactivate')) {
completer.resolve(false);
}
});
rtr.navigateByUrl('/a').then((_) => {
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('canDeactivate {A}');
expect(log).toEqual(['canDeactivate: /can-deactivate -> /a']);
expect(fixture.debugElement.nativeElement).toHaveText('routerCanDeactivate {A}');
expect(log).toEqual(['routerCanDeactivate: /can-deactivate -> /a']);
async.done();
});
});
@@ -294,10 +295,10 @@ export function main() {
.then((_) => rtr.navigateByUrl('/activation-hooks/child'))
.then((_) => {
expect(log).toEqual([
'canActivate child: null -> /child',
'canActivate parent: null -> /activation-hooks',
'onActivate parent: null -> /activation-hooks',
'onActivate child: null -> /child'
'routerCanActivate child: null -> /child',
'routerCanActivate parent: null -> /activation-hooks',
'routerOnActivate parent: null -> /activation-hooks',
'routerOnActivate child: null -> /child'
]);
log = [];
@@ -305,10 +306,10 @@ export function main() {
})
.then((_) => {
expect(log).toEqual([
'canDeactivate parent: /activation-hooks -> /a',
'canDeactivate child: /child -> null',
'onDeactivate child: /child -> null',
'onDeactivate parent: /activation-hooks -> /a'
'routerCanDeactivate parent: /activation-hooks -> /a',
'routerCanDeactivate child: /child -> null',
'routerOnDeactivate child: /child -> null',
'routerOnDeactivate parent: /activation-hooks -> /a'
]);
async.done();
});
@@ -320,11 +321,13 @@ export function main() {
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
.then((_) => rtr.navigateByUrl('/reuse-hooks/1'))
.then((_) => {
expect(log).toEqual(
['canActivate: null -> /reuse-hooks/1', 'onActivate: null -> /reuse-hooks/1']);
expect(log).toEqual([
'routerCanActivate: null -> /reuse-hooks/1',
'routerOnActivate: null -> /reuse-hooks/1'
]);
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('canReuse')) {
if (ev.startsWith('routerCanReuse')) {
completer.resolve(true);
}
});
@@ -335,8 +338,8 @@ export function main() {
})
.then((_) => {
expect(log).toEqual([
'canReuse: /reuse-hooks/1 -> /reuse-hooks/2',
'onReuse: /reuse-hooks/1 -> /reuse-hooks/2'
'routerCanReuse: /reuse-hooks/1 -> /reuse-hooks/2',
'routerOnReuse: /reuse-hooks/1 -> /reuse-hooks/2'
]);
async.done();
});
@@ -347,11 +350,13 @@ export function main() {
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
.then((_) => rtr.navigateByUrl('/reuse-hooks/1'))
.then((_) => {
expect(log).toEqual(
['canActivate: null -> /reuse-hooks/1', 'onActivate: null -> /reuse-hooks/1']);
expect(log).toEqual([
'routerCanActivate: null -> /reuse-hooks/1',
'routerOnActivate: null -> /reuse-hooks/1'
]);
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
if (ev.startsWith('canReuse')) {
if (ev.startsWith('routerCanReuse')) {
completer.resolve(false);
}
});
@@ -361,11 +366,11 @@ export function main() {
})
.then((_) => {
expect(log).toEqual([
'canReuse: /reuse-hooks/1 -> /reuse-hooks/2',
'canActivate: /reuse-hooks/1 -> /reuse-hooks/2',
'canDeactivate: /reuse-hooks/1 -> /reuse-hooks/2',
'onDeactivate: /reuse-hooks/1 -> /reuse-hooks/2',
'onActivate: /reuse-hooks/1 -> /reuse-hooks/2'
'routerCanReuse: /reuse-hooks/1 -> /reuse-hooks/2',
'routerCanActivate: /reuse-hooks/1 -> /reuse-hooks/2',
'routerCanDeactivate: /reuse-hooks/1 -> /reuse-hooks/2',
'routerOnDeactivate: /reuse-hooks/1 -> /reuse-hooks/2',
'routerOnActivate: /reuse-hooks/1 -> /reuse-hooks/2'
]);
async.done();
});
@@ -393,7 +398,7 @@ function logHook(name: string, next: ComponentInstruction, prev: ComponentInstru
@Component({selector: 'activate-cmp', template: 'activate cmp'})
class ActivateCmp implements OnActivate {
onActivate(next: ComponentInstruction, prev: ComponentInstruction) {
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('activate', next, prev);
}
}
@@ -405,7 +410,7 @@ class ActivateCmp implements OnActivate {
})
@RouteConfig([new Route({path: '/child-activate', component: ActivateCmp})])
class ParentActivateCmp implements OnActivate {
onActivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
completer = PromiseWrapper.completer();
logHook('parent activate', next, prev);
return completer.promise;
@@ -414,14 +419,14 @@ class ParentActivateCmp implements OnActivate {
@Component({selector: 'deactivate-cmp', template: 'deactivate cmp'})
class DeactivateCmp implements OnDeactivate {
onDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('deactivate', next, prev);
}
}
@Component({selector: 'deactivate-cmp', template: 'deactivate cmp'})
class WaitDeactivateCmp implements OnDeactivate {
onDeactivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
completer = PromiseWrapper.completer();
logHook('deactivate', next, prev);
return completer.promise;
@@ -435,7 +440,7 @@ class WaitDeactivateCmp implements OnDeactivate {
})
@RouteConfig([new Route({path: '/child-deactivate', component: WaitDeactivateCmp})])
class ParentDeactivateCmp implements OnDeactivate {
onDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('parent deactivate', next, prev);
}
}
@@ -449,8 +454,10 @@ class ParentDeactivateCmp implements OnDeactivate {
class ReuseCmp implements OnReuse,
CanReuse {
constructor() { cmpInstanceCount += 1; }
canReuse(next: ComponentInstruction, prev: ComponentInstruction) { return true; }
onReuse(next: ComponentInstruction, prev: ComponentInstruction) { logHook('reuse', next, prev); }
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) { return true; }
routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('reuse', next, prev);
}
}
@Component({
@@ -462,58 +469,61 @@ class ReuseCmp implements OnReuse,
class NeverReuseCmp implements OnReuse,
CanReuse {
constructor() { cmpInstanceCount += 1; }
canReuse(next: ComponentInstruction, prev: ComponentInstruction) { return false; }
onReuse(next: ComponentInstruction, prev: ComponentInstruction) { logHook('reuse', next, prev); }
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) { return false; }
routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('reuse', next, prev);
}
}
@Component({
selector: 'can-activate-cmp',
template: `canActivate {<router-outlet></router-outlet>}`,
template: `routerCanActivate {<router-outlet></router-outlet>}`,
directives: [RouterOutlet]
})
@RouteConfig([new Route({path: '/a', component: A}), new Route({path: '/b', component: B})])
@CanActivate(CanActivateCmp.canActivate)
@CanActivate(CanActivateCmp.routerCanActivate)
class CanActivateCmp {
static canActivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<boolean> {
static routerCanActivate(next: ComponentInstruction,
prev: ComponentInstruction): Promise<boolean> {
completer = PromiseWrapper.completer();
logHook('canActivate', next, prev);
logHook('routerCanActivate', next, prev);
return completer.promise;
}
}
@Component({
selector: 'can-deactivate-cmp',
template: `canDeactivate {<router-outlet></router-outlet>}`,
template: `routerCanDeactivate {<router-outlet></router-outlet>}`,
directives: [RouterOutlet]
})
@RouteConfig([new Route({path: '/a', component: A}), new Route({path: '/b', component: B})])
class CanDeactivateCmp implements CanDeactivate {
canDeactivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<boolean> {
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<boolean> {
completer = PromiseWrapper.completer();
logHook('canDeactivate', next, prev);
logHook('routerCanDeactivate', next, prev);
return completer.promise;
}
}
@Component({selector: 'all-hooks-child-cmp', template: `child`})
@CanActivate(AllHooksChildCmp.canActivate)
@CanActivate(AllHooksChildCmp.routerCanActivate)
class AllHooksChildCmp implements CanDeactivate, OnDeactivate, OnActivate {
canDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('canDeactivate child', next, prev);
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerCanDeactivate child', next, prev);
return true;
}
onDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onDeactivate child', next, prev);
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnDeactivate child', next, prev);
}
static canActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('canActivate child', next, prev);
static routerCanActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerCanActivate child', next, prev);
return true;
}
onActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onActivate child', next, prev);
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnActivate child', next, prev);
}
}
@@ -523,57 +533,57 @@ class AllHooksChildCmp implements CanDeactivate, OnDeactivate, OnActivate {
directives: [RouterOutlet]
})
@RouteConfig([new Route({path: '/child', component: AllHooksChildCmp})])
@CanActivate(AllHooksParentCmp.canActivate)
@CanActivate(AllHooksParentCmp.routerCanActivate)
class AllHooksParentCmp implements CanDeactivate,
OnDeactivate, OnActivate {
canDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('canDeactivate parent', next, prev);
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerCanDeactivate parent', next, prev);
return true;
}
onDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onDeactivate parent', next, prev);
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnDeactivate parent', next, prev);
}
static canActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('canActivate parent', next, prev);
static routerCanActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerCanActivate parent', next, prev);
return true;
}
onActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onActivate parent', next, prev);
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnActivate parent', next, prev);
}
}
@Component({selector: 'reuse-hooks-cmp', template: 'reuse hooks cmp'})
@CanActivate(ReuseHooksCmp.canActivate)
@CanActivate(ReuseHooksCmp.routerCanActivate)
class ReuseHooksCmp implements OnActivate, OnReuse, OnDeactivate, CanReuse, CanDeactivate {
canReuse(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
completer = PromiseWrapper.completer();
logHook('canReuse', next, prev);
logHook('routerCanReuse', next, prev);
return completer.promise;
}
onReuse(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onReuse', next, prev);
routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnReuse', next, prev);
}
canDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('canDeactivate', next, prev);
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerCanDeactivate', next, prev);
return true;
}
onDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onDeactivate', next, prev);
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnDeactivate', next, prev);
}
static canActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('canActivate', next, prev);
static routerCanActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerCanActivate', next, prev);
return true;
}
onActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('onActivate', next, prev);
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
logHook('routerOnActivate', next, prev);
}
}
+2 -2
View File
@@ -238,8 +238,8 @@ class DummyParentComp {
function makeDummyOutlet() {
var ref = new SpyRouterOutlet();
ref.spy('canActivate').andCallFake((_) => PromiseWrapper.resolve(true));
ref.spy('canReuse').andCallFake((_) => PromiseWrapper.resolve(false));
ref.spy('canDeactivate').andCallFake((_) => PromiseWrapper.resolve(true));
ref.spy('routerCanReuse').andCallFake((_) => PromiseWrapper.resolve(false));
ref.spy('routerCanDeactivate').andCallFake((_) => PromiseWrapper.resolve(true));
ref.spy('activate').andCallFake((_) => PromiseWrapper.resolve(true));
return ref;
}
@@ -131,11 +131,11 @@ export function main() {
template: "ignore: {{ignore}}; " +
"literal: {{literal}}; interpolate: {{interpolate}}; " +
"oneWayA: {{oneWayA}}; oneWayB: {{oneWayB}}; " +
"twoWayA: {{twoWayA}}; twoWayB: {{twoWayB}}; ({{onChangesCount}})"
"twoWayA: {{twoWayA}}; twoWayB: {{twoWayB}}; ({{ngOnChangesCount}})"
})
.Class({
constructor: function() {
this.onChangesCount = 0;
this.ngOnChangesCount = 0;
this.ignore = '-';
this.literal = '?';
this.interpolate = '?';
@@ -148,7 +148,7 @@ export function main() {
this.twoWayAEmitter = new EventEmitter();
this.twoWayBEmitter = new EventEmitter();
},
onChanges: function(changes) {
ngOnChanges: function(changes) {
var assert = (prop, value) => {
if (this[prop] != value) {
throw new Error(
@@ -168,7 +168,7 @@ export function main() {
}
};
switch (this.onChangesCount++) {
switch (this.ngOnChangesCount++) {
case 0:
assert('ignore', '-');
assertChange('literal', 'Text');