X Tutup
Skip to content
Closed

Cleanup #4144

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class ChangeDetectorRef {
*/
detach(): void { this._cd.mode = ChangeDetectionStrategy.Detached; }

detectChanges(): void { this._cd.detectChanges(); }

/**
* Reattach the change detector to the change detector tree.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
library angular2.src.core.compiler.directive_lifecycle_reflector;

import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/core/compiler/interfaces.dart';

bool hasLifecycleHook(interface, type) {
if (type is! Type) return false;
const INTERFACES = const {
LifecycleHooks.OnInit: OnInit,
LifecycleHooks.OnDestroy: OnDestroy,
LifecycleHooks.DoCheck: DoCheck,
LifecycleHooks.OnChanges: OnChanges,
LifecycleHooks.AfterContentInit: AfterContentInit,
LifecycleHooks.AfterContentChecked: AfterContentChecked,
LifecycleHooks.AfterViewInit: AfterViewInit,
LifecycleHooks.AfterViewChecked: AfterViewChecked,
};

return reflector.interfaces(type).contains(interface);
bool hasLifecycleHook(LifecycleHooks interface, token) {
if (token is! Type) return false;
Type interfaceType = INTERFACES[interface];
return reflector.interfaces(token).contains(interfaceType);
}
24 changes: 12 additions & 12 deletions modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {Type} from 'angular2/src/core/facade/lang';
import * as Interfaces from './interfaces';
import {LifecycleHooks} from './interfaces';

export function hasLifecycleHook(lcInterface, type): boolean {
if (!(type instanceof Type)) return false;
export function hasLifecycleHook(lcInterface: LifecycleHooks, token): boolean {
if (!(token instanceof Type)) return false;

var proto = (<any>type).prototype;
var proto = (<any>token).prototype;

switch (lcInterface) {
case Interfaces.AfterContentInit:
case LifecycleHooks.AfterContentInit:
return !!proto.afterContentInit;
case Interfaces.AfterContentChecked:
case LifecycleHooks.AfterContentChecked:
return !!proto.afterContentChecked;
case Interfaces.AfterViewInit:
case LifecycleHooks.AfterViewInit:
return !!proto.afterViewInit;
case Interfaces.AfterViewChecked:
case LifecycleHooks.AfterViewChecked:
return !!proto.afterViewChecked;
case Interfaces.OnChanges:
case LifecycleHooks.OnChanges:
return !!proto.onChanges;
case Interfaces.DoCheck:
case LifecycleHooks.DoCheck:
return !!proto.doCheck;
case Interfaces.OnDestroy:
case LifecycleHooks.OnDestroy:
return !!proto.onDestroy;
case Interfaces.OnInit:
case LifecycleHooks.OnInit:
return !!proto.onInit;
default:
return false;
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/src/core/compiler/element_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {RenderDirectiveMetadata} from 'angular2/src/core/render/api';
import {EventConfig} from 'angular2/src/core/render/event_config';
import {PipeBinding} from '../pipes/pipe_binding';

import * as LifecycleHooks from './interfaces';
import {LifecycleHooks} from './interfaces';

var _staticKeys;

Expand Down
45 changes: 20 additions & 25 deletions modules/angular2/src/core/compiler/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {StringMap} from 'angular2/src/core/facade/collection';

export enum LifecycleHooks {
OnInit,
OnDestroy,
DoCheck,
OnChanges,
AfterContentInit,
AfterContentChecked,
AfterViewInit,
AfterViewChecked
}

/**
* Lifecycle hooks are guaranteed to be called in the following order:
* - `OnChanges` (if any bindings have changed),
Expand Down Expand Up @@ -38,9 +49,7 @@ import {StringMap} from 'angular2/src/core/facade/collection';
* }
* ```
*/
export class OnChanges {
onChanges(changes: StringMap<string, any>): void {}
}
export interface OnChanges { onChanges(changes: StringMap<string, any>); }

/**
* Notify a directive when it has been checked the first time.
Expand All @@ -54,15 +63,13 @@ export class OnChanges {
*
* ```
* @Component(...)
* class MyComponent @implements OnInit {
* class MyComponent implements OnInit {
* onInit(): void {
* }
* }
* ```
*/
export class OnInit {
onInit(): void {}
}
export interface OnInit { onInit(); }

/**
* Overrides the default change detection.
Expand All @@ -83,9 +90,7 @@ export class OnInit {
* }
* ```
*/
export class DoCheck {
doCheck(): void {}
}
export interface DoCheck { doCheck(); }

/**
* Notify a directive whenever a {@link ViewMetadata} that contains it is destroyed.
Expand All @@ -101,9 +106,7 @@ export class DoCheck {
* }
* ```
*/
export class OnDestroy {
onDestroy(): void {}
}
export interface OnDestroy { onDestroy(); }

/**
* Notify a directive when the bindings of all its content children have been checked the first
Expand All @@ -119,9 +122,7 @@ export class OnDestroy {
* }
* ```
*/
export class AfterContentInit {
afterContentInit(): void {}
}
export interface AfterContentInit { afterContentInit(); }

/**
* Notify a directive when the bindings of all its content children have been checked (whether
Expand All @@ -137,9 +138,7 @@ export class AfterContentInit {
* }
* ```
*/
export class AfterContentChecked {
afterContentChecked(): void {}
}
export interface AfterContentChecked { afterContentChecked(); }

/**
* Notify a directive when the bindings of all its view children have been checked the first time
Expand All @@ -155,9 +154,7 @@ export class AfterContentChecked {
* }
* ```
*/
export class AfterViewInit {
afterViewInit(): void {}
}
export interface AfterViewInit { afterViewInit(); }

/**
* Notify a directive when the bindings of all its view children have been checked (whether they
Expand All @@ -173,6 +170,4 @@ export class AfterViewInit {
* }
* ```
*/
export class AfterViewChecked {
afterViewChecked(): void {}
}
export interface AfterViewChecked { afterViewChecked(); }
2 changes: 1 addition & 1 deletion modules/angular2/src/test_lib/test_lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export class SpyObject {
}
}
}
// Noop so that SpyObject has the smae interface as in Dart
// Noop so that SpyObject has the same interface as in Dart
noSuchMethod(args) {}

spy(name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
ddescribe,
describe,
it,
iit,
xit,
expect,
beforeEach,
afterEach,
tick,
fakeAsync
} from 'angular2/test_lib';

import {ChangeDetectorRef} from 'angular2/src/core/change_detection/change_detector_ref';
import {SpyChangeDetector} from 'angular2/test/core/spies';
import {ChangeDetector} from 'angular2/src/core/change_detection/interfaces';


export function main() {
describe('ChangeDetectorRef', () => {
it('should delegate detectChanges()', () => {
var changeDetector = new SpyChangeDetector();
changeDetector.spy('detectChanges');
var changeDetectorRef = new ChangeDetectorRef(<any>changeDetector);
changeDetectorRef.detectChanges();
expect(changeDetector.spy('detectChanges')).toHaveBeenCalledWith();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use toHaveBeenCalled()

});
});
}
X Tutup