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

docs(dependency-injection): add Dart version, tweak TS version #972

Closed
wants to merge 20 commits into from
Closed
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
16 changes: 16 additions & 0 deletions public/docs/_examples/dependency-injection/dart/.analysis_options
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Supported lint rules and documentation: http://dart-lang.github.io/linter/lints/
linter:
rules:
- always_declare_return_types
- camel_case_types
- empty_constructor_bodies
- annotate_overrides
- avoid_init_to_null
- constant_identifier_names
- one_member_abstracts
- slash_for_doc_comments
- sort_constructors_first
- unnecessary_brace_in_string_interp

analyzer:
# strong-mode: true
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
// #docplaster

// #docregion

// #docregion imports
import 'package:angular2/core.dart';

import 'app_config.dart';
import 'car/car_component.dart';
import 'heroes/heroes_component.dart';
import 'app_config.dart';
import 'logger_service.dart';
import 'user_service.dart';

//PENDING: check whether we intend to hide injector_component.dart & providers_component.dart; if so, change docregion name?
// #enddocregion imports
import 'injector_component.dart';
import 'providers_component.dart';

@Component(
selector: 'my-app',
template: '''
<h1>{{title}}</h1>
<my-car></my-car>
<my-injectors></my-injectors>
<my-tests></my-tests>
<h2>User</h2>
<p id="user">
{{userInfo}}
<button (click)=\'nextUser()\'>Next User</button>
<p>
<my-heroes id="authorized" *ngIf="isAuthorized"></my-heroes>
<my-heroes id="unauthorized" *ngIf="!isAuthorized"></my-heroes>
''',
directives: const [CarComponent, HeroesComponent, InjectorComponent, ProvidersComponent],
<h1>{{title}}</h1>
<my-car></my-car>
<my-injectors></my-injectors>
<my-tests></my-tests>
<h2>User</h2>
<p id="user">
{{userInfo}}
<button (click)="nextUser()">Next User</button>
<p>
<my-heroes id="authorized" *ngIf="isAuthorized"></my-heroes>
<my-heroes id="unauthorized" *ngIf="!isAuthorized"></my-heroes>''',
directives: const [
CarComponent,
HeroesComponent,
InjectorComponent,
ProvidersComponent
],
// #docregion providers
providers: const [Logger, UserService, const Provider(Config, useValue: CONFIG)]
providers: const [
Logger,
UserService,
const Provider(AppConfig, useValue: config1)]
// #enddocregion providers
)
)
class AppComponent {
UserService _userService;
String title;
final UserService _userService;
final String title;

//#docregion ctor
Copy link
Contributor

Choose a reason for hiding this comment

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

: title = config.title;

AppComponent(Config config, this._userService) {
title = config.title;
}

AppComponent(AppConfig config, this._userService)
: title = config.title;
// #enddocregion ctor

bool get isAuthorized {
return user.isAuthorized;
}
Expand All @@ -56,8 +61,7 @@ class AppComponent {
return _userService.user;
}

String get userInfo {
return 'Current user, ${user.name}, is ${isAuthorized ? "" : "not"} authorized. ';
}
String get userInfo => 'Current user, ${user.name}, is'
'${isAuthorized ? "" : " not"} authorized. ';
}
// #enddocregion
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

// #docregion
import 'package:angular2/core.dart';

import 'car/car_component.dart';
import 'heroes/heroes_component_1.dart';

@Component(
selector: 'my-app',
template: '''
<h1>{{title}}</h1>
<my-car></my-car>
<my-heroes></my-heroes>
''',
<h1>{{title}}</h1>
<my-car></my-car>
<my-heroes></my-heroes>''',
directives: const [CarComponent, HeroesComponent])
class AppComponent {
var title = 'Dependency Injection';
final String title = 'Dependency Injection';
}
// #enddocregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@

// #docregion imports
import 'package:angular2/core.dart';

import 'app_config.dart';
import 'car/car_component.dart';
import 'heroes/heroes_component_1.dart';
import 'app_config.dart';
import 'logger_service.dart';

// #enddocregion imports

@Component(
selector: 'my-app',
template: '''
<h1>{{title}}</h1>
<my-car></my-car>
<my-heroes></my-heroes>
''',
<h1>{{title}}</h1>
<my-car></my-car>
<my-heroes></my-heroes>''',
directives: const [
CarComponent,
HeroesComponent
],
providers: const [
Logger,
// #docregion provider-config
const Provider('app.config', useValue: CONFIG)
const Provider(AppConfig, useValue: config1)
])
class AppComponent {
String title;
final String title;

// #docregion ctor
AppComponent(@Inject('app.config') Config config) {
title = config.title;
}
AppComponent(AppConfig config)
: title = config.title;
// #enddocregion
}
// #enddocregion
32 changes: 10 additions & 22 deletions public/docs/_examples/dependency-injection/dart/lib/app_config.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
//#docregion

// #docregion
// #docregion token
import 'package:angular2/core.dart';

const APP_CONFIG = const OpaqueToken('app.config');
// #enddocregion token

//#docregion config
abstract class Config {
final String apiEndpoint;
//#docregion const-class
@Injectable()
class AppConfig {
final apiEndpoint;
final String title;

const Config({this.apiEndpoint, this.title});
const AppConfig(this.apiEndpoint, this.title);
}
//#enddocregion const-class

class ConfigImpl implements Config {
final String apiEndpoint;
final String title;

const ConfigImpl({this.apiEndpoint, this.title});
}

const CONFIG = const ConfigImpl(apiEndpoint: 'api.heroes.com', title: 'Dependency Injection');
//#enddocregion config

//#docregion config-hash
const CONFIG_HASH = const {'apiEndpoint': 'api.heroes.com', 'title': 'Dependency Injection'};
//#enddocregion config-hash
//#docregion const-object
const config1 = const AppConfig('api.heroes.com', 'Dependency Injection');
//#enddocregion const-object
12 changes: 7 additions & 5 deletions public/docs/_examples/dependency-injection/dart/lib/car/car.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// #docregion

import 'package:angular2/core.dart';

@Injectable()
// #docregion engine
class Engine {
int cylinders = 4;
final int cylinders = 4;
}
// #enddocregion engine

@Injectable()
// #docregion tires
class Tires {
String make = 'Flintstone';
Expand All @@ -18,15 +19,16 @@ class Tires {
@Injectable()
class Car {
//#docregion car-ctor
Engine engine;
Tires tires;
final Engine engine;
final Tires tires;
String description = 'DI';

Car(this.engine, this.tires);

// #enddocregion car-ctor

// Method using the engine and tires
String drive() => '$description car with ${engine.cylinders} cylinders and ${tires.make} tires.';
String drive() => '$description car with ${engine.cylinders} cylinders'
' and ${tires.make} tires.';
}
// #enddocregion car
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// #docregion

import 'package:angular2/core.dart';

import 'car.dart';
import 'car_no_di.dart' as carNoDi;
import 'car_factory.dart';
import 'car_creations.dart' as carCreations;
import 'car_factory.dart';
import 'car_injector.dart';
import 'car_no_di.dart' as carNoDi;

@Component(
selector: 'my-car',
template: '''
<h2>Cars</h2>
<div id="di">{{car.drive()}}</div>
<div id="nodi">{{noDiCar.drive()}}</div>
<div id="injector">{{injectorCar.drive()}}</div>
<div id="factory">{{factoryCar.drive()}}</div>
<div id="simple">{{simpleCar.drive()}}</div>
<div id="super">{{superCar.drive()}}</div>
<div id="test">{{testCar.drive()}}</div>
''',
<h2>Cars</h2>
<div id="di">{{car.drive()}}</div>
<div id="nodi">{{noDiCar.drive()}}</div>
<div id="injector">{{injectorCar.drive()}}</div>
<div id="factory">{{factoryCar.drive()}}</div>
<div id="simple">{{simpleCar.drive()}}</div>
<div id="super">{{superCar.drive()}}</div>
<div id="test">{{testCar.drive()}}</div>''',
providers: const [Car, Engine, Tires])
class CarComponent {
Car car;
final Car car;

CarComponent(this.car);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: newline after ctor.


CarComponent(this.car) {}
Car factoryCar = (new CarFactory()).createCar();
Car injectorCar = useInjector();
carNoDi.Car noDiCar = new carNoDi.Car();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Examples with car and engine variations

// #docplaster
// Examples with car and engine variations
import 'car.dart';

///////// example 1 ////////////
Car simpleCar() {
//#docregion car-ctor-instantiation

// Simple car with 4 cylinders and Flintstone tires.
var car = new Car(new Engine(), new Tires());
//#enddocregion car-ctor-instantiation
Expand All @@ -17,27 +15,26 @@ Car simpleCar() {

//#docregion car-ctor-instantiation-with-param
class Engine2 implements Engine {
int cylinders;
final int cylinders;

Engine2(this.cylinders);
}

//#enddocregion car-ctor-instantiation-with-param
Car superCar() {
//#docregion car-ctor-instantiation-with-param

// Super car with 12 cylinders and Flintstone tires.
var bigCylinders = 12;
var car = new Car(new Engine2(bigCylinders), new Tires());
//#enddocregion car-ctor-instantiation-with-param
Car superCar() {
//#docregion car-ctor-instantiation-with-param
// Super car with 12 cylinders and Flintstone tires.
var bigCylinders = 12;
var car = new Car(new Engine2(bigCylinders), new Tires());
//#enddocregion car-ctor-instantiation-with-param
car.description = 'Super';
return car;
}
/////////// example 3 //////////

//#docregion car-ctor-instantiation-with-mocks
class MockEngine extends Engine {
int cylinders = 8;
final int cylinders = 8;
}

class MockTires extends Tires {
Expand All @@ -46,11 +43,10 @@ class MockTires extends Tires {

//#enddocregion car-ctor-instantiation-with-mocks
Car testCar() {
//#docregion car-ctor-instantiation-with-mocks

// Test car with 8 cylinders and YokoGoodStone tires.
var car = new Car(new MockEngine(), new MockTires());
//#enddocregion car-ctor-instantiation-with-mocks
//#docregion car-ctor-instantiation-with-mocks
// Test car with 8 cylinders and YokoGoodStone tires.
var car = new Car(new MockEngine(), new MockTires());
//#enddocregion car-ctor-instantiation-with-mocks
car.description = 'Test';
return car;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// #docregion

import 'car.dart';

// BAD pattern!
class CarFactory {
Car createCar() {
var car = new Car(createEngine(), createTires());
car.description = 'Factory';
return car;
return new Car(createEngine(), createTires())
..description = 'Factory';
}

Engine createEngine() => new Engine();

Tires createTires() => new Tires();
}
Loading