X Tutup
Skip to content

Commit 4bb9c46

Browse files
committed
fix(core): Provide setDevMode() to enable/disable development mode in Javascript.
1 parent 7343ef0 commit 4bb9c46

File tree

9 files changed

+53
-1
lines changed

9 files changed

+53
-1
lines changed

modules/angular2/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './src/common/forms';
2020
export * from './src/core/debug';
2121
export * from './src/core/change_detection';
2222
export * from './src/core/ambient';
23+
export * from './src/core/dev_mode';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// #docregion enableDevMode
2+
import {bootstrap, enableDevMode} from 'angular2/core';
3+
import {MyComponent} from 'my_component';
4+
5+
enableDevMode();
6+
bootstrap(MyComponent);
7+
// #enddocregion
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Component} from 'angular2/core';
2+
3+
@Component({selector: 'my-component', template: '<h1>My Component</h1>'})
4+
export class MyComponent {
5+
}

modules/angular2/manual_typings/globals-es6.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface BrowserNodeGlobal {
2424
zone: Zone;
2525
getAngularTestability: Function;
2626
getAllAngularTestabilities: Function;
27+
angularDevMode: boolean;
2728
setTimeout: Function;
2829
clearTimeout: Function;
2930
setInterval: Function;

modules/angular2/src/core/application_ref.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {Compiler_} from "./linker/compiler";
4848
import {wtfLeave, wtfCreateScope, WtfScopeFn} from './profile/profile';
4949
import {ChangeDetectorRef} from 'angular2/src/core/change_detection/change_detector_ref';
5050
import {AMBIENT_DIRECTIVES, AMBIENT_PIPES} from "angular2/src/core/ambient";
51+
import {lockDevMode} from 'angular2/src/core/facade/lang';
5152
import {COMMON_DIRECTIVES, COMMON_PIPES} from "angular2/common";
5253

5354
/**
@@ -126,6 +127,7 @@ var _platform: PlatformRef;
126127

127128
export function platformCommon(providers?: Array<Type | Provider | any[]>,
128129
initializer?: () => void): PlatformRef {
130+
lockDevMode();
129131
if (isPresent(_platform)) {
130132
if (isBlank(providers)) {
131133
return _platform;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Dart development mode is determined by checked mode.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {enableDevMode} from 'angular2/src/core/facade/lang';

modules/angular2/src/core/facade/lang.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ bool isJsObject(o) {
241241
return false;
242242
}
243243

244+
void lockDevMode() {
245+
// lockDevMode() has no effect in Dart.
246+
}
247+
248+
void enableDevMode() {
249+
// enableDevMode() has no effect in Dart.
250+
}
251+
244252
bool assertionsEnabled() {
245253
var k = false;
246254
assert((k = true));

modules/angular2/src/core/facade/lang.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,34 @@ export function getTypeNameForDebugging(type: Type): string {
3939
export var Math = _global.Math;
4040
export var Date = _global.Date;
4141

42+
var _devMode: boolean = !!_global.angularDevMode;
43+
var _devModeLocked: boolean = false;
44+
45+
export function lockDevMode() {
46+
_devModeLocked = true;
47+
}
48+
49+
/**
50+
* Enable Angular's development mode, which turns on assertions and other
51+
* checks within the framework.
52+
*
53+
* One important assertion this enables verifies that a change detection pass
54+
* does not result in additional changes to any bindings (also known as
55+
* unidirectional data flow).
56+
*
57+
* {@example core/ts/dev_mode/dev_mode_example.ts region='enableDevMode'}
58+
*/
59+
export function enableDevMode() {
60+
// TODO(alxhub): Refactor out of facade/lang as per issue #5157.
61+
if (_devModeLocked) {
62+
// Cannot use BaseException as that ends up importing from facade/lang.
63+
throw 'Cannot enable dev mode after platform setup.';
64+
}
65+
_devMode = true;
66+
}
67+
4268
export function assertionsEnabled(): boolean {
43-
return false;
69+
return _devMode;
4470
}
4571

4672
// TODO: remove calls to assert in production environment

0 commit comments

Comments
 (0)
X Tutup