Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 10877bd

Browse files
committed
docs(dependency-injection): add Dart version, tweak TS version
closes #972
1 parent e178214 commit 10877bd

33 files changed

+866
-500
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Supported lint rules and documentation: http://dart-lang.github.io/linter/lints/
2+
linter:
3+
rules:
4+
- always_declare_return_types
5+
- camel_case_types
6+
- empty_constructor_bodies
7+
- annotate_overrides
8+
- avoid_init_to_null
9+
- constant_identifier_names
10+
- one_member_abstracts
11+
- slash_for_doc_comments
12+
- sort_constructors_first
13+
- unnecessary_brace_in_string_interp
14+
15+
analyzer:
16+
# strong-mode: true
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,54 @@
11
// #docplaster
2-
32
// #docregion
4-
53
// #docregion imports
64
import 'package:angular2/core.dart';
5+
6+
import 'app_config.dart';
77
import 'car/car_component.dart';
88
import 'heroes/heroes_component.dart';
9-
import 'app_config.dart';
109
import 'logger_service.dart';
1110
import 'user_service.dart';
12-
11+
//PENDING: check whether we intend to hide injector_component.dart & providers_component.dart; if so, change docregion name?
1312
// #enddocregion imports
1413
import 'injector_component.dart';
1514
import 'providers_component.dart';
1615

1716
@Component(
1817
selector: 'my-app',
1918
template: '''
20-
<h1>{{title}}</h1>
21-
<my-car></my-car>
22-
<my-injectors></my-injectors>
23-
<my-tests></my-tests>
24-
<h2>User</h2>
25-
<p id="user">
26-
{{userInfo}}
27-
<button (click)=\'nextUser()\'>Next User</button>
28-
<p>
29-
<my-heroes id="authorized" *ngIf="isAuthorized"></my-heroes>
30-
<my-heroes id="unauthorized" *ngIf="!isAuthorized"></my-heroes>
31-
''',
32-
directives: const [CarComponent, HeroesComponent, InjectorComponent, ProvidersComponent],
19+
<h1>{{title}}</h1>
20+
<my-car></my-car>
21+
<my-injectors></my-injectors>
22+
<my-tests></my-tests>
23+
<h2>User</h2>
24+
<p id="user">
25+
{{userInfo}}
26+
<button (click)="nextUser()">Next User</button>
27+
<p>
28+
<my-heroes id="authorized" *ngIf="isAuthorized"></my-heroes>
29+
<my-heroes id="unauthorized" *ngIf="!isAuthorized"></my-heroes>''',
30+
directives: const [
31+
CarComponent,
32+
HeroesComponent,
33+
InjectorComponent,
34+
ProvidersComponent
35+
],
3336
// #docregion providers
34-
providers: const [Logger, UserService, const Provider(Config, useValue: CONFIG)]
37+
providers: const [
38+
Logger,
39+
UserService,
40+
const Provider(AppConfig, useValue: config1)]
3541
// #enddocregion providers
36-
)
42+
)
3743
class AppComponent {
38-
UserService _userService;
39-
String title;
44+
final UserService _userService;
45+
final String title;
4046

4147
//#docregion ctor
42-
AppComponent(Config config, this._userService) {
43-
title = config.title;
44-
}
45-
48+
AppComponent(AppConfig config, this._userService)
49+
: title = config.title;
4650
// #enddocregion ctor
51+
4752
bool get isAuthorized {
4853
return user.isAuthorized;
4954
}
@@ -56,8 +61,7 @@ class AppComponent {
5661
return _userService.user;
5762
}
5863

59-
String get userInfo {
60-
return 'Current user, ${user.name}, is ${isAuthorized ? "" : "not"} authorized. ';
61-
}
64+
String get userInfo => 'Current user, ${user.name}, is'
65+
'${isAuthorized ? "" : " not"} authorized. ';
6266
}
6367
// #enddocregion

public/docs/_examples/dependency-injection/dart/lib/app_component_1.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
// #docregion
44
import 'package:angular2/core.dart';
5+
56
import 'car/car_component.dart';
67
import 'heroes/heroes_component_1.dart';
78

89
@Component(
910
selector: 'my-app',
1011
template: '''
11-
<h1>{{title}}</h1>
12-
<my-car></my-car>
13-
<my-heroes></my-heroes>
14-
''',
12+
<h1>{{title}}</h1>
13+
<my-car></my-car>
14+
<my-heroes></my-heroes>''',
1515
directives: const [CarComponent, HeroesComponent])
1616
class AppComponent {
17-
var title = 'Dependency Injection';
17+
final String title = 'Dependency Injection';
1818
}
1919
// #enddocregion
2020

public/docs/_examples/dependency-injection/dart/lib/app_component_2.dart

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@
22

33
// #docregion imports
44
import 'package:angular2/core.dart';
5+
6+
import 'app_config.dart';
57
import 'car/car_component.dart';
68
import 'heroes/heroes_component_1.dart';
7-
import 'app_config.dart';
89
import 'logger_service.dart';
9-
1010
// #enddocregion imports
11+
1112
@Component(
1213
selector: 'my-app',
1314
template: '''
14-
<h1>{{title}}</h1>
15-
<my-car></my-car>
16-
<my-heroes></my-heroes>
17-
''',
15+
<h1>{{title}}</h1>
16+
<my-car></my-car>
17+
<my-heroes></my-heroes>''',
1818
directives: const [
1919
CarComponent,
2020
HeroesComponent
2121
],
2222
providers: const [
2323
Logger,
24-
// #docregion provider-config
25-
const Provider('app.config', useValue: CONFIG)
24+
const Provider(AppConfig, useValue: config1)
2625
])
2726
class AppComponent {
28-
String title;
27+
final String title;
2928

3029
// #docregion ctor
31-
AppComponent(@Inject('app.config') Config config) {
32-
title = config.title;
33-
}
30+
AppComponent(AppConfig config)
31+
: title = config.title;
32+
// #enddocregion
3433
}
35-
// #enddocregion
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
//#docregion
2-
1+
// #docregion
32
// #docregion token
43
import 'package:angular2/core.dart';
54

6-
const APP_CONFIG = const OpaqueToken('app.config');
7-
// #enddocregion token
8-
9-
//#docregion config
10-
abstract class Config {
11-
final String apiEndpoint;
5+
//#docregion const-class
6+
@Injectable()
7+
class AppConfig {
8+
final apiEndpoint;
129
final String title;
1310

14-
const Config({this.apiEndpoint, this.title});
11+
const AppConfig(this.apiEndpoint, this.title);
1512
}
13+
//#enddocregion const-class
1614

17-
class ConfigImpl implements Config {
18-
final String apiEndpoint;
19-
final String title;
20-
21-
const ConfigImpl({this.apiEndpoint, this.title});
22-
}
23-
24-
const CONFIG = const ConfigImpl(apiEndpoint: 'api.heroes.com', title: 'Dependency Injection');
25-
//#enddocregion config
26-
27-
//#docregion config-hash
28-
const CONFIG_HASH = const {'apiEndpoint': 'api.heroes.com', 'title': 'Dependency Injection'};
29-
//#enddocregion config-hash
15+
//#docregion const-object
16+
const config1 = const AppConfig('api.heroes.com', 'Dependency Injection');
17+
//#enddocregion const-object
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// #docregion
2-
32
import 'package:angular2/core.dart';
43

4+
@Injectable()
55
// #docregion engine
66
class Engine {
7-
int cylinders = 4;
7+
final int cylinders = 4;
88
}
99
// #enddocregion engine
1010

11+
@Injectable()
1112
// #docregion tires
1213
class Tires {
1314
String make = 'Flintstone';
@@ -18,15 +19,16 @@ class Tires {
1819
@Injectable()
1920
class Car {
2021
//#docregion car-ctor
21-
Engine engine;
22-
Tires tires;
22+
final Engine engine;
23+
final Tires tires;
2324
String description = 'DI';
2425

2526
Car(this.engine, this.tires);
2627

2728
// #enddocregion car-ctor
2829

2930
// Method using the engine and tires
30-
String drive() => '$description car with ${engine.cylinders} cylinders and ${tires.make} tires.';
31+
String drive() => '$description car with ${engine.cylinders} cylinders'
32+
' and ${tires.make} tires.';
3133
}
3234
// #enddocregion car

public/docs/_examples/dependency-injection/dart/lib/car/car_component.dart

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
// #docregion
2-
32
import 'package:angular2/core.dart';
3+
44
import 'car.dart';
5-
import 'car_no_di.dart' as carNoDi;
6-
import 'car_factory.dart';
75
import 'car_creations.dart' as carCreations;
6+
import 'car_factory.dart';
87
import 'car_injector.dart';
8+
import 'car_no_di.dart' as carNoDi;
99

1010
@Component(
1111
selector: 'my-car',
1212
template: '''
13-
<h2>Cars</h2>
14-
<div id="di">{{car.drive()}}</div>
15-
<div id="nodi">{{noDiCar.drive()}}</div>
16-
<div id="injector">{{injectorCar.drive()}}</div>
17-
<div id="factory">{{factoryCar.drive()}}</div>
18-
<div id="simple">{{simpleCar.drive()}}</div>
19-
<div id="super">{{superCar.drive()}}</div>
20-
<div id="test">{{testCar.drive()}}</div>
21-
''',
13+
<h2>Cars</h2>
14+
<div id="di">{{car.drive()}}</div>
15+
<div id="nodi">{{noDiCar.drive()}}</div>
16+
<div id="injector">{{injectorCar.drive()}}</div>
17+
<div id="factory">{{factoryCar.drive()}}</div>
18+
<div id="simple">{{simpleCar.drive()}}</div>
19+
<div id="super">{{superCar.drive()}}</div>
20+
<div id="test">{{testCar.drive()}}</div>''',
2221
providers: const [Car, Engine, Tires])
2322
class CarComponent {
24-
Car car;
23+
final Car car;
24+
25+
CarComponent(this.car);
2526

26-
CarComponent(this.car) {}
2727
Car factoryCar = (new CarFactory()).createCar();
2828
Car injectorCar = useInjector();
2929
carNoDi.Car noDiCar = new carNoDi.Car();
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// Examples with car and engine variations
2-
31
// #docplaster
2+
// Examples with car and engine variations
43
import 'car.dart';
54

65
///////// example 1 ////////////
76
Car simpleCar() {
87
//#docregion car-ctor-instantiation
9-
108
// Simple car with 4 cylinders and Flintstone tires.
119
var car = new Car(new Engine(), new Tires());
1210
//#enddocregion car-ctor-instantiation
@@ -17,27 +15,26 @@ Car simpleCar() {
1715
1816
//#docregion car-ctor-instantiation-with-param
1917
class Engine2 implements Engine {
20-
int cylinders;
18+
final int cylinders;
2119

2220
Engine2(this.cylinders);
2321
}
24-
2522
//#enddocregion car-ctor-instantiation-with-param
26-
Car superCar() {
27-
//#docregion car-ctor-instantiation-with-param
2823

29-
// Super car with 12 cylinders and Flintstone tires.
30-
var bigCylinders = 12;
31-
var car = new Car(new Engine2(bigCylinders), new Tires());
32-
//#enddocregion car-ctor-instantiation-with-param
24+
Car superCar() {
25+
//#docregion car-ctor-instantiation-with-param
26+
// Super car with 12 cylinders and Flintstone tires.
27+
var bigCylinders = 12;
28+
var car = new Car(new Engine2(bigCylinders), new Tires());
29+
//#enddocregion car-ctor-instantiation-with-param
3330
car.description = 'Super';
3431
return car;
3532
}
3633
/////////// example 3 //////////
3734
3835
//#docregion car-ctor-instantiation-with-mocks
3936
class MockEngine extends Engine {
40-
int cylinders = 8;
37+
final int cylinders = 8;
4138
}
4239

4340
class MockTires extends Tires {
@@ -46,11 +43,10 @@ class MockTires extends Tires {
4643

4744
//#enddocregion car-ctor-instantiation-with-mocks
4845
Car testCar() {
49-
//#docregion car-ctor-instantiation-with-mocks
50-
51-
// Test car with 8 cylinders and YokoGoodStone tires.
52-
var car = new Car(new MockEngine(), new MockTires());
53-
//#enddocregion car-ctor-instantiation-with-mocks
46+
//#docregion car-ctor-instantiation-with-mocks
47+
// Test car with 8 cylinders and YokoGoodStone tires.
48+
var car = new Car(new MockEngine(), new MockTires());
49+
//#enddocregion car-ctor-instantiation-with-mocks
5450
car.description = 'Test';
5551
return car;
5652
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// #docregion
2-
32
import 'car.dart';
43

4+
// BAD pattern!
55
class CarFactory {
66
Car createCar() {
7-
var car = new Car(createEngine(), createTires());
8-
car.description = 'Factory';
9-
return car;
7+
return new Car(createEngine(), createTires())
8+
..description = 'Factory';
109
}
1110

1211
Engine createEngine() => new Engine();
13-
1412
Tires createTires() => new Tires();
1513
}

0 commit comments

Comments
 (0)