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

Commit 48580e9

Browse files
thelgevoldwardbell
authored andcommitted
docs: add DI cookbook
1 parent 3b768a3 commit 48580e9

28 files changed

+558
-0
lines changed
Lines changed: 41 additions & 0 deletions
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+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.js
Lines changed: 47 additions & 0 deletions
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+
}
Lines changed: 27 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 9 additions & 0 deletions
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+
}
Lines changed: 27 additions & 0 deletions
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+
}
Lines changed: 28 additions & 0 deletions
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+
}
Lines changed: 27 additions & 0 deletions
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+
}
Lines changed: 5 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)