refactor(LifecycleEvent): remove LifecycleEvent

fixes #3924

BREAKING CHANGE

The `lifecycle` configuration for directive has been dropped.

Before

    // Dart
    @Component({lifecycle: const [LifecycleEvent.OnChanges], ...})
    class MyComponent implements OnChanges {
      void onChanges() {...}
    }

    // Typescript
    @Component({lifecycle: [LifecycleEvent.OnChanges], ...})
    class MyComponent implements OnChanges {
      onChanges(): void {...}
    }

    // ES5
    var MyComponent = ng.
    Component({lifecycle: [LifecycleEvent.OnChanges], ...}).
    Class({
      onChanges: function() {...}
    });

After

    // Dart
    @Component({...})
    class MyComponent implements OnChanges {
      void onChanges() {...}
    }

    // Typescript
    @Component({...})
    class MyComponent implements OnChanges {
      onChanges(): void {...}
    }

    // ES5
    var MyComponent = ng
      .Component({...})
      .Class({
        onChanges: function() {
        }
      });
This commit is contained in:
Victor Berchet
2015-08-31 18:32:32 -07:00
parent 67b9414268
commit 8302afffb4
42 changed files with 311 additions and 842 deletions

View File

@ -16,22 +16,10 @@ main() {
.callOnChanges).toBe(true);
});
it("should be true when the lifecycle includes onChanges", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.OnChanges]))
.callOnChanges).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callOnChanges)
.toBe(false);
});
it("should be false when empty lifecycle", () {
expect(metadata(
DirectiveImplementingOnChanges, new Directive(lifecycle: []))
.callOnChanges).toBe(false);
});
});
describe("onDestroy", () {
@ -40,12 +28,6 @@ main() {
.callOnDestroy).toBe(true);
});
it("should be true when the lifecycle includes onDestroy", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.OnDestroy]))
.callOnDestroy).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callOnDestroy)
.toBe(false);
@ -58,12 +40,6 @@ main() {
.callDoCheck).toBe(true);
});
it("should be true when the lifecycle includes doCheck", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.DoCheck]))
.callDoCheck).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callDoCheck)
.toBe(false);
@ -76,12 +52,6 @@ main() {
.callOnInit).toBe(true);
});
it("should be true when the lifecycle includes onInit", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.OnInit])).callOnInit)
.toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callOnInit)
.toBe(false);
@ -95,12 +65,6 @@ main() {
.callAfterContentInit).toBe(true);
});
it("should be true when the lifecycle includes afterContentInit", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.AfterContentInit]))
.callAfterContentInit).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive())
.callAfterContentInit).toBe(false);
@ -114,12 +78,6 @@ main() {
.callAfterContentChecked).toBe(true);
});
it("should be true when the lifecycle includes afterContentChecked", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.AfterContentChecked]))
.callAfterContentChecked).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive())
.callAfterContentChecked).toBe(false);
@ -133,12 +91,6 @@ main() {
.callAfterViewInit).toBe(true);
});
it("should be true when the lifecycle includes afterViewInit", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.AfterViewInit]))
.callAfterViewInit).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive())
.callAfterViewInit).toBe(false);
@ -152,12 +104,6 @@ main() {
.callAfterViewChecked).toBe(true);
});
it("should be true when the lifecycle includes afterViewChecked", () {
expect(metadata(DirectiveNoHooks,
new Directive(lifecycle: [LifecycleEvent.AfterViewChecked]))
.callAfterViewChecked).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive())
.callAfterViewChecked).toBe(false);

View File

@ -13,7 +13,7 @@ import {
proxy
} from 'angular2/test_lib';
import {DirectiveMetadata, LifecycleEvent} from 'angular2/src/core/metadata';
import {DirectiveMetadata} from 'angular2/src/core/metadata';
import {DirectiveBinding} from 'angular2/src/core/compiler/element_injector';
import {RenderDirectiveMetadata} from 'angular2/src/core/render/api';
@ -30,22 +30,9 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes onChanges", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.OnChanges]}))
.callOnChanges)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callOnChanges).toBe(false);
});
it("should be false when empty lifecycle", () => {
expect(metadata(DirectiveWithOnChangesMethod, new DirectiveMetadata({lifecycle: []}))
.callOnChanges)
.toBe(false);
});
});
describe("onDestroy", () => {
@ -54,13 +41,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes onDestroy", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.OnDestroy]}))
.callOnDestroy)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callOnDestroy).toBe(false);
});
@ -72,13 +52,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes onDestroy", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.OnInit]}))
.callOnInit)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callOnInit).toBe(false);
});
@ -90,13 +63,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes doCheck", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.DoCheck]}))
.callDoCheck)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callDoCheck).toBe(false);
});
@ -109,13 +75,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes afterContentInit", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.AfterContentInit]}))
.callAfterContentInit)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callAfterContentInit)
.toBe(false);
@ -129,13 +88,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes afterContentChecked", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.AfterContentChecked]}))
.callAfterContentChecked)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callAfterContentChecked)
.toBe(false);
@ -150,13 +102,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes afterViewInit", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.AfterViewInit]}))
.callAfterViewInit)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callAfterViewInit).toBe(false);
});
@ -169,13 +114,6 @@ export function main() {
.toBe(true);
});
it("should be true when the lifecycle includes afterViewChecked", () => {
expect(metadata(DirectiveNoHooks,
new DirectiveMetadata({lifecycle: [LifecycleEvent.AfterViewChecked]}))
.callAfterViewChecked)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new DirectiveMetadata()).callAfterViewChecked)
.toBe(false);
@ -200,7 +138,7 @@ class DirectiveWithOnCheckMethod {
}
class DirectiveWithOnDestroyMethod {
onDestroy(_) {}
onDestroy() {}
}
class DirectiveWithAfterContentInitMethod {

View File

@ -20,7 +20,7 @@ import {
import {Injector} from 'angular2/di';
import {NgIf} from 'angular2/directives';
import {Component, View, ViewMetadata, LifecycleEvent} from 'angular2/metadata';
import {Component, View, ViewMetadata, OnDestroy} from 'angular2/metadata';
import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader';
import {ElementRef} from 'angular2/src/core/compiler/element_ref';
import {DOCUMENT} from 'angular2/src/core/render/render';
@ -262,13 +262,9 @@ class ChildComp {
class DynamicallyCreatedComponentService {}
@Component({
selector: 'hello-cmp',
viewBindings: [DynamicallyCreatedComponentService],
lifecycle: [LifecycleEvent.OnDestroy]
})
@Component({selector: 'hello-cmp', viewBindings: [DynamicallyCreatedComponentService]})
@View({template: "{{greeting}}"})
class DynamicallyCreatedCmp {
class DynamicallyCreatedCmp implements OnDestroy {
greeting: string;
dynamicallyCreatedComponentService: DynamicallyCreatedComponentService;
destroyed: boolean = false;

View File

@ -36,7 +36,7 @@ import {
ViewQuery,
ComponentMetadata,
DirectiveMetadata,
LifecycleEvent
OnDestroy
} from 'angular2/metadata';
import {bind, Injector, Binding, Optional, Inject, Injectable, Self, SkipSelf, InjectMetadata, Host, HostMetadata, SkipSelfMetadata} from 'angular2/di';
import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref';
@ -217,7 +217,7 @@ class B_Needs_A {
constructor(dep) {}
}
class DirectiveWithDestroy {
class DirectiveWithDestroy implements OnDestroy {
onDestroyCounter: number;
constructor() { this.onDestroyCounter = 0; }
@ -832,7 +832,7 @@ export function main() {
it("should call onDestroy on directives subscribed to this event", () => {
var inj = injector(ListWrapper.concat(
[DirectiveBinding.createFromType(DirectiveWithDestroy,
new DirectiveMetadata({lifecycle: [LifecycleEvent.OnDestroy]}))],
new DirectiveMetadata())],
extraBindings));
var destroy = inj.get(DirectiveWithDestroy);
inj.dehydrate();

View File

@ -266,8 +266,6 @@ class NoPropertyAccess {
@Component(
selector: 'on-change',
// TODO: needed because of https://github.com/angular/angular/issues/2120
lifecycle: const [LifecycleEvent.OnChanges],
properties: const ['prop'])
@View(template: '')
class OnChangeComponent implements OnChanges {
@ -300,8 +298,7 @@ class ComponentWithObservableList {
}
@Directive(
selector: 'directive-logging-checks',
lifecycle: const [LifecycleEvent.DoCheck])
selector: 'directive-logging-checks')
class DirectiveLoggingChecks implements DoCheck {
Log log;

View File

@ -13,7 +13,19 @@ import {
TestComponentBuilder
} from 'angular2/test_lib';
import {Directive, Component, View, ViewMetadata, LifecycleEvent} from 'angular2/metadata';
import {
Directive,
Component,
View,
ViewMetadata,
OnChanges,
OnInit,
DoCheck,
AfterContentInit,
AfterContentChecked,
AfterViewInit,
AfterViewChecked
} from 'angular2/metadata';
export function main() {
describe('directive lifecycle integration spec', () => {
@ -47,27 +59,16 @@ export function main() {
}
@Directive({selector: '[lifecycle-dir]', lifecycle: [LifecycleEvent.DoCheck]})
class LifecycleDir {
@Directive({selector: '[lifecycle-dir]'})
class LifecycleDir implements DoCheck {
constructor(private _log: Log) {}
doCheck() { this._log.add("child_doCheck"); }
}
@Component({
selector: "[lifecycle]",
properties: ['field'],
lifecycle: [
LifecycleEvent.OnChanges,
LifecycleEvent.OnInit,
LifecycleEvent.DoCheck,
LifecycleEvent.AfterContentInit,
LifecycleEvent.AfterContentChecked,
LifecycleEvent.AfterViewInit,
LifecycleEvent.AfterViewChecked
]
})
@Component({selector: "[lifecycle]", properties: ['field']})
@View({template: `<div lifecycle-dir></div>`, directives: [LifecycleDir]})
class LifecycleCmp {
class LifecycleCmp implements OnChanges, OnInit, DoCheck, AfterContentInit, AfterContentChecked,
AfterViewInit, AfterViewChecked {
field;
constructor(private _log: Log) {}