-
Notifications
You must be signed in to change notification settings - Fork 875
[WIP] Dart dependency injection #951
Changes from all commits
4284c45
3fe4cd4
ca9631c
35ee7fc
7114540
2a89adb
f61c19e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
// #docplaster | ||
|
||
// #docregion | ||
|
||
// #docregion imports | ||
import 'package:angular2/core.dart'; | ||
import 'package:angular2/angular2.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(Config, useValue: CONFIG) | ||
] | ||
// #enddocregion providers | ||
) | ||
) | ||
class AppComponent { | ||
UserService _userService; | ||
String title; | ||
|
@@ -57,7 +64,8 @@ class AppComponent { | |
} | ||
|
||
String get userInfo { | ||
return 'Current user, ${user.name}, is ${isAuthorized ? "" : "not"} authorized. '; | ||
return 'Current user, ${user.name}, is' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use arrow syntax here. |
||
'${isAuthorized ? "" : " not"} authorized. '; | ||
} | ||
} | ||
// #enddocregion |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
// Early versions | ||
|
||
// #docregion | ||
import 'package:angular2/core.dart'; | ||
import 'package:angular2/angular2.dart'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are the imports changed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed that recommendation. Can you tell me more about it? When talking to an internal engineer who uses Angular (but isn't on the Angular team), it seemed to us that angular2.dart was better because you wouldn't have to think twice about which files to include. (And tree shaking would make it not matter if you included more than you needed.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zoechi Would you be able to dig up that recommendation? Would be very interested to see the rationale. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea where exactly I saw this. It was a while back when the public API was reworked about 2 months ago. I don't think it's too important. |
||
|
||
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'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,10 @@ class Car { | |
engine = new Engine(); | ||
tires = new Tires(); | ||
} | ||
|
||
//#enddocregion car-ctor | ||
|
||
// Method using the engine and tires | ||
drive() => '$description car with ${engine.cylinders} cylinders and ${tires.make} tires.'; | ||
drive() => '$description car with ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return type. Also, this is against the general Dart style guide as we're using an action verb but not actually performing any changes.. Can we consider renaming this and making it a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll check, but I believe we're just following the original TypeScript example here. We follow Dart conventions where possible (e.g. using types for APIs), while trying to keep the code similar (e.g. names). |
||
'${engine.cylinders} cylinders and ${tires.make} tires.'; | ||
} | ||
//#enddocregion car |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
// #docregion | ||
|
||
class Hero { | ||
num id; | ||
String name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
|
||
import 'package:angular2/core.dart'; | ||
import 'hero.dart'; | ||
import 'hero_service.dart'; | ||
|
||
@Component( | ||
selector: 'hero-list', | ||
template: ''' | ||
<div *ngFor="#hero of heroes"> | ||
{{hero.id}} - {{hero.name}} | ||
({{hero.isSecret ? \'secret\' : \'public\'}}) | ||
</div> | ||
''') | ||
<div *ngFor="#hero of heroes"> | ||
{{hero.id}} - {{hero.name}} | ||
({{hero.isSecret ? \'secret\' : \'public\'}}) | ||
</div>''') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. Thanks for catching that. |
||
class HeroListComponent { | ||
List<Hero> heroes; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
|
||
import 'package:angular2/core.dart'; | ||
import 'hero.dart'; | ||
import 'mock_heroes.dart'; | ||
|
||
@Component( | ||
selector: 'hero-list', | ||
template: ''' | ||
<div *ngFor="#hero of heroes"> | ||
{{hero.id}} - {{hero.name}} | ||
</div> | ||
''') | ||
<div *ngFor="#hero of heroes"> | ||
{{hero.id}} - {{hero.name}} | ||
</div>''') | ||
class HeroListComponent { | ||
List<Hero> heroes = HEROES; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
|
||
import 'package:angular2/core.dart'; | ||
import 'hero.dart'; | ||
import 'hero_service.dart'; | ||
|
||
@Component( | ||
selector: 'hero-list', | ||
template: ''' | ||
<div *ngFor="#hero of heroes"> | ||
{{hero.id}} - {{hero.name}} | ||
</div> | ||
''') | ||
<div *ngFor="#hero of heroes"> | ||
{{hero.id}} - {{hero.name}} | ||
</div>''') | ||
class HeroListComponent { | ||
List<Hero> heroes; | ||
|
||
//#docregion ctor | ||
HeroListComponent(HeroService heroService) : heroes = heroService.getHeroes(); | ||
HeroListComponent(HeroService heroService) | ||
: heroes = heroService.getHeroes(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make |
||
//#enddocregion ctor | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
// #docregion | ||
|
||
import 'hero.dart'; | ||
import 'mock_heroes.dart'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
|
||
import 'package:angular2/core.dart'; | ||
import 'hero_service.dart'; | ||
import '../logger_service.dart'; | ||
import '../user_service.dart'; | ||
import 'hero_service.dart'; | ||
|
||
// #docregion factory | ||
heroServiceFactory(Logger logger, UserService userService) => | ||
new HeroService(logger, userService.user.isAuthorized); | ||
// #enddocregion factory | ||
|
||
// #docregion provider | ||
const heroServiceProvider = | ||
const Provider(HeroService, useFactory: heroServiceFactory, deps: const [Logger, UserService]); | ||
const heroServiceProvider = const Provider(HeroService, | ||
useFactory: heroServiceFactory, | ||
deps: const [Logger, UserService]); | ||
// #enddocregion provider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
\
seems redundant. Why single quote while elsewhere in the template double quotes are used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't notice that. You're right,
"
is better.