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

Commit dcdfad3

Browse files
thelgevoldwardbell
authored andcommitted
docs: add DI cookbook
1 parent 3ec0e71 commit dcdfad3

28 files changed

+558
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
describe('Dependency Injection', function () {
2+
3+
beforeAll(function () {
4+
browser.get('');
5+
});
6+
7+
it('should render DI samples', function () {
8+
var loggedInUser = element.all(by.xpath('//h3[text()="Logged in user"]')).get(0);
9+
expect(loggedInUser).toBeDefined();
10+
11+
loggedInUser = element.all(by.xpath('//div[text()="Name: Bombasto"]')).get(0);
12+
expect(loggedInUser).toBeDefined();
13+
14+
var sortedHeroes = element.all(by.xpath('//h3[text()="Sorted Heroes" and position()=1]')).get(0);
15+
expect(sortedHeroes).toBeDefined();
16+
17+
var sortedHero = element.all(by.xpath('//sorted-heroes/[text()="Mr. Nice" and position()=2]')).get(0);
18+
expect(sortedHero).toBeDefined();
19+
20+
sortedHero = element.all(by.xpath('//sorted-heroes/[text()="RubberMan" and position()=3]')).get(0);
21+
expect(sortedHero).toBeDefined();
22+
23+
sortedHero = element.all(by.xpath('//sorted-heroes/[text()="Magma"]')).get(0);
24+
expect(sortedHero).toBeDefined();
25+
26+
var heroOfTheMonth = element.all(by.xpath('//h3[text()="Hero of the month"]')).get(0);
27+
expect(heroOfTheMonth).toBeDefined();
28+
29+
var heroBios = element.all(by.xpath('//h3[text()="Hero Bios"]')).get(0);
30+
expect(heroBios).toBeDefined();
31+
32+
var magmaText = element.all(by.xpath('//textarea[text()="Hero of all trades"]')).get(0);
33+
expect(magmaText).toBeDefined();
34+
35+
var magmaPhone = element.all(by.xpath('//div[text()="Phone #: 555-555-5555"]')).get(0);
36+
expect(magmaPhone).toBeDefined();
37+
38+
var runnersUp = element.all(by.xpath('//h4[text()="Other candidates RubberMan, Mr. Nice"]')).get(0);
39+
expect(runnersUp).toBeDefined();
40+
});
41+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// #docregion
2+
import {Component,OnInit} from 'angular2/core';
3+
import {LoggerService} from './logger.service';
4+
import {UserContext} from './user-context.service';
5+
import {Heroes} from './hero-bios.component';
6+
import {SortedHeroes} from './sorted-heroes.component';
7+
import {HeroOfTheMonth} from './hero-of-the-month.component';
8+
9+
@Component({
10+
selector: 'my-app',
11+
directives:[Heroes,SortedHeroes,HeroOfTheMonth],
12+
template:
13+
`<h1>DI Components</h1>
14+
<div class="di-component">
15+
<h3>Logged in user</h3>
16+
<div>Name: {{_userContext.name}}</div>
17+
<div>Role: {{_userContext.role}}</div>
18+
</div>
19+
20+
<div class="di-component">
21+
<h3>Sorted Heroes</h3>
22+
<sorted-heroes></sorted-heroes>
23+
</div>
24+
25+
<div class="di-component">
26+
<h3>Hero of the month</h3>
27+
<hero-of-the-month></hero-of-the-month>
28+
</div>
29+
30+
<div class="di-component">
31+
<h3>Hero Bios</h3>
32+
<hero-bios></hero-bios>
33+
</div>`
34+
})
35+
36+
export class AppComponent implements OnInit {
37+
38+
private userId:number = 1;
39+
40+
constructor(private _logger:LoggerService, private _userContext:UserContext){
41+
this._userContext.loadUser(this.userId);
42+
}
43+
44+
ngOnInit(){
45+
this._logger.logInfo('AppComponent initialized');
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #docregion
2+
import {Component,Input,OnInit} from 'angular2/core';
3+
import {Hero} from './hero';
4+
import {HeroService} from './hero.service';
5+
6+
@Component({
7+
selector:'bio',
8+
template:`<h4>{{hero.name}}</h4>
9+
<div>
10+
<ng-content></ng-content>
11+
<textarea cols="25" [(ngModel)]="hero.description">{{hero.description}}</textarea>
12+
</div>`,
13+
providers:[HeroService]
14+
})
15+
16+
export class Bio implements OnInit{
17+
18+
@Input() heroIndex:number;
19+
private hero:Hero;
20+
21+
constructor(private _heroService:HeroService){
22+
}
23+
24+
ngOnInit(){
25+
this.hero = this._heroService.getHeroById(this.heroIndex);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// #docregion
2+
import {Component, Host, Optional, OnInit, ElementRef} from 'angular2/core';
3+
4+
import {HeroService} from './hero.service';
5+
6+
@Component({
7+
selector:'contact-details',
8+
template:'<div>Phone #: {{phoneNumber}}</div>'
9+
})
10+
11+
export class ContactDetails implements OnInit{
12+
13+
private phoneNumber:string;
14+
constructor(@Optional() @Host() private _heroService:HeroService, elementRef:ElementRef){
15+
let nativeElement = elementRef.nativeElement;
16+
}
17+
18+
ngOnInit(){
19+
if(this._heroService){
20+
this.phoneNumber = this._heroService.currentHero.phone;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #docregion
2+
import {Injectable} from 'angular2/core';
3+
4+
@Injectable()
5+
export class DateLoggerService{
6+
logInfo(msg:string){
7+
console.log(new Date().toString() + ` INFO: ${msg}`);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
import {Bio} from './bio.component';
4+
import {ContactDetails} from './contact-details.component';
5+
6+
@Component({
7+
template:`<div>
8+
<bio [heroIndex]="0">
9+
<contact-details></contact-details>
10+
</bio>
11+
</div>
12+
<div>
13+
<bio [heroIndex]="1">
14+
<contact-details></contact-details>
15+
</bio>
16+
</div>
17+
<div>
18+
<bio [heroIndex]="2">
19+
<contact-details></contact-details>
20+
</bio>
21+
</div>`,
22+
selector:'hero-bios',
23+
directives:[Bio,ContactDetails]
24+
})
25+
26+
export class Heroes{
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #docregion
2+
import {Component,provide} from 'angular2/core';
3+
import {LoggerService} from './logger.service';
4+
import {Hero} from './hero';
5+
import {HeroService} from './hero.service';
6+
import {DateLoggerService} from './date-logger.service';
7+
import {RunnersUp} from './runners-up';
8+
import {runnersUpFactory} from './runners-up-provider.service';
9+
10+
@Component({
11+
selector:'hero-of-the-month',
12+
template:`<div>Winner: <strong>{{_heroOfTheMonth.name}}</strong></div>
13+
<div>Reason for award: <strong>{{_heroOfTheMonth.description}}</strong></div>
14+
<h4>Other candidates {{_runnersUp.names}}</h4>`,
15+
16+
providers:[
17+
HeroService,
18+
provide(Hero, {useValue:new Hero('Magma','Had a great month!','555-555-5555')}),
19+
provide(LoggerService, {useClass:DateLoggerService}),
20+
provide(RunnersUp, {useFactory:runnersUpFactory, deps:[Hero, HeroService]})
21+
]
22+
})
23+
24+
export class HeroOfTheMonth{
25+
constructor(logger:LoggerService, private _heroOfTheMonth:Hero, private _runnersUp:RunnersUp){
26+
logger.logInfo('starting up');
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #docregion
2+
import {Hero} from './hero';
3+
import {Injectable} from 'angular2/core';
4+
5+
@Injectable()
6+
export class HeroService{
7+
8+
currentHero:Hero;
9+
10+
//TODO move to database
11+
private _heros:Array<Hero> = [new Hero('RubberMan','Hero of many talents', '123-456-7899'),
12+
new Hero('Magma','Hero of all trades', '555-555-5555'),
13+
new Hero('Mr. Nice','The name says it all','111-222-3333')];
14+
15+
getHeroById(index:number):Hero{
16+
if(!this.currentHero){
17+
let heroes = this.getAllHeroes();
18+
this.currentHero = heroes[index];
19+
}
20+
21+
return this.currentHero;
22+
}
23+
24+
getAllHeroes():Array<Hero>{
25+
return this._heros;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// #docregion
2+
export class Hero{
3+
constructor(public name:string, public description:string, public phone:string){
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// #docregion
2+
import {Injectable} from 'angular2/core';
3+
4+
@Injectable()
5+
export class LoggerService{
6+
7+
logInfo(msg:string){
8+
console.log(`INFO: ${msg}`);
9+
}
10+
11+
logDebug(msg:string){
12+
console.log(`DEBUG: ${msg}`);
13+
}
14+
15+
logError(msg:string){
16+
console.log(`ERROR: ${msg}`);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #docregion
2+
import {bootstrap} from 'angular2/platform/browser';
3+
import {AppComponent} from './app.component';
4+
import {LoggerService} from './logger.service';
5+
import {UserContext} from './user-context.service';
6+
import {UserService} from './user.service';
7+
8+
bootstrap(AppComponent, [LoggerService,UserService,UserContext])
9+
.catch((err:any) => console.error(err));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #docregion
2+
import {Hero} from './hero';
3+
import {HeroService} from './hero.service';
4+
import {RunnersUp} from './runners-up';
5+
6+
export const runnersUpFactory = (winner:Hero, heroService:HeroService) => {
7+
let names:string = heroService.getAllHeroes()
8+
.filter((hero) => hero.name !== winner.name)
9+
.map((hero) => hero.name).join(', ');
10+
11+
return new RunnersUp(names);
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// #docregion
2+
import {Injectable} from 'angular2/core';
3+
4+
@Injectable()
5+
export class RunnersUp{
6+
names:string;
7+
8+
constructor(names:string){
9+
this.names = names;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #docregion
2+
import {HeroService} from './hero.service';
3+
import {Hero} from './hero';
4+
5+
export class SortedHeroesBase{
6+
7+
sortedHeroes:Array<Hero>;
8+
9+
constructor(private heroService:HeroService){
10+
this.sortedHeroes = heroService.getAllHeroes();
11+
12+
this.sortedHeroes.sort((h1,h2) => {
13+
if(h1.name < h2.name){
14+
return -1;
15+
}
16+
if(h1.name > h2.name){
17+
return 1
18+
};
19+
return 0;
20+
});
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
import {SortedHeroesBase} from './sorted-heroes-base';
4+
import {HeroService} from './hero.service';
5+
6+
@Component({
7+
selector:'sorted-heroes',
8+
template:`<div *ngFor="#hero of sortedHeroes">{{hero.name}}</div>`,
9+
providers:[HeroService]
10+
})
11+
12+
export class SortedHeroes extends SortedHeroesBase{
13+
constructor(heroService:HeroService){
14+
super(heroService);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// #docregion
2+
import {UserService} from './user.service';
3+
import {Injectable} from 'angular2/core';
4+
import {LoggerService} from './logger.service';
5+
6+
@Injectable()
7+
export class UserContext{
8+
9+
name:string;
10+
role:string;
11+
loggedInSince:Date;
12+
13+
constructor(private _userService:UserService, private _loggerService:LoggerService){
14+
this.loggedInSince = new Date();
15+
}
16+
17+
loadUser(userId:number){
18+
let user = this._userService.getUserById(userId);
19+
this.name = user.name;
20+
this.role = user.role;
21+
22+
this._loggerService.logDebug('loaded User');
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #docregion
2+
import {Injectable} from 'angular2/core';
3+
4+
@Injectable()
5+
export class UserService{
6+
7+
getUserById(userId:number):any{
8+
return {name:'Bombasto',role:'Admin'};
9+
}
10+
}

public/docs/_examples/cb-dependency-injection/ts/example-config.json

Whitespace-only changes.

0 commit comments

Comments
 (0)