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

Commit c1f2c39

Browse files
thelgevoldwardbell
authored andcommitted
docs: add DI cookbook
1 parent 56ed5e3 commit c1f2c39

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2027
-27
lines changed

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var _exampleBoilerplateFiles = [
7474
'package.json',
7575
'styles.css',
7676
'tsconfig.json',
77+
'tslint.json',
7778
'typings.json'
7879
];
7980

public/docs/_examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ package.json
66
karma.conf.js
77
karma-test-shim.js
88
tsconfig.json
9+
tslint.json
910
npm-debug*.
1011
**/protractor.config.js

public/docs/_examples/attribute-directives/ts/app/highlight.directive.2.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ import {Directive, ElementRef, Input} from 'angular2/core';
1212
})
1313

1414
export class HighlightDirective {
15+
1516
// #docregion ctor
16-
constructor(private el: ElementRef) { }
17+
private _el:HTMLElement;
18+
constructor(el: ElementRef) { this._el = el.nativeElement; }
1719
// #enddocregion ctor
1820

1921
// #docregion mouse-methods
2022
onMouseEnter() { this._highlight("yellow"); }
2123
onMouseLeave() { this._highlight(null); }
2224

2325
private _highlight(color: string) {
24-
this.el.nativeElement.style.backgroundColor = color;
26+
this._el.style.backgroundColor = color;
2527
}
2628
// #enddocregion mouse-methods
2729

public/docs/_examples/attribute-directives/ts/app/highlight.directive.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {Directive, ElementRef, Input} from 'angular2/core';
1212

1313
// #docregion class-1
1414
export class HighlightDirective {
15+
16+
private _defaultColor = 'red';
17+
private _el:HTMLElement;
1518
// #enddocregion class-1
1619
// #enddocregion full
1720
/*
@@ -20,29 +23,29 @@ export class HighlightDirective {
2023
// #enddocregion highlight
2124
*/
2225
// #docregion full
26+
27+
// #docregion defaultColor
28+
@Input() set defaultColor(colorName:string){
29+
this._defaultColor = colorName || this._defaultColor;
30+
}
31+
// #enddocregion defaultColor
2332
// #docregion class-1
33+
2434
// #docregion color
2535
@Input('myHighlight') highlightColor: string;
2636
// #enddocregion color
2737

28-
private _defaultColor = 'red';
2938
// #enddocregion class-1
30-
// #docregion defaultColor
31-
@Input() set defaultColor(colorName:string){
32-
this._defaultColor = colorName || this._defaultColor;
33-
}
34-
// #enddocregion defaultColor
3539
// #docregion class-1
36-
37-
constructor(private el: ElementRef) { }
40+
constructor(el: ElementRef) { this._el = el.nativeElement; }
3841

3942
// #docregion mouse-enter
4043
onMouseEnter() { this._highlight(this.highlightColor || this._defaultColor); }
4144
// #enddocregion mouse-enter
4245
onMouseLeave() { this._highlight(null); }
4346

4447
private _highlight(color:string) {
45-
this.el.nativeElement.style.backgroundColor = color;
48+
this._el.style.backgroundColor = color;
4649
}
4750
}
4851
// #enddocregion class-1
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
describe('Dependency Injection Cookbook', function () {
2+
3+
beforeAll(function () {
4+
browser.get('');
5+
});
6+
7+
it('should render Logged in User example', function () {
8+
var loggedInUser = element.all(by.xpath('//h3[text()="Logged in user"]')).get(0);
9+
expect(loggedInUser).toBeDefined();
10+
});
11+
12+
it('"Bombasto" should be the logged in user', function () {
13+
loggedInUser = element.all(by.xpath('//div[text()="Name: Bombasto"]')).get(0);
14+
expect(loggedInUser).toBeDefined();
15+
});
16+
17+
it('should render sorted heroes', function () {
18+
var sortedHeroes = element.all(by.xpath('//h3[text()="Sorted Heroes" and position()=1]')).get(0);
19+
expect(sortedHeroes).toBeDefined();
20+
});
21+
22+
it('Mr. Nice should be in sorted heroes', function () {
23+
var sortedHero = element.all(by.xpath('//sorted-heroes/[text()="Mr. Nice" and position()=2]')).get(0);
24+
expect(sortedHero).toBeDefined();
25+
});
26+
27+
it('RubberMan should be in sorted heroes', function () {
28+
sortedHero = element.all(by.xpath('//sorted-heroes/[text()="RubberMan" and position()=3]')).get(0);
29+
expect(sortedHero).toBeDefined();
30+
});
31+
32+
it('Magma should be in sorted heroes', function () {
33+
sortedHero = element.all(by.xpath('//sorted-heroes/[text()="Magma"]')).get(0);
34+
expect(sortedHero).toBeDefined();
35+
});
36+
37+
it('should render Hero of the Month', function () {
38+
var heroOfTheMonth = element.all(by.xpath('//h3[text()="Hero of the month"]')).get(0);
39+
expect(heroOfTheMonth).toBeDefined();
40+
});
41+
42+
it('should render Hero Bios', function () {
43+
var heroBios = element.all(by.xpath('//h3[text()="Hero Bios"]')).get(0);
44+
expect(heroBios).toBeDefined();
45+
});
46+
47+
it('should render Magma\'s description in Hero Bios', function () {
48+
var magmaText = element.all(by.xpath('//textarea[text()="Hero of all trades"]')).get(0);
49+
expect(magmaText).toBeDefined();
50+
});
51+
52+
it('should render Magma\'s phone in Hero Bios and Contacts', function () {
53+
var magmaPhone = element.all(by.xpath('//div[text()="Phone #: 555-555-5555"]')).get(0);
54+
expect(magmaPhone).toBeDefined();
55+
});
56+
57+
it('should render Hero-of-the-Month runner-ups', function () {
58+
var runnersUp = element(by.id('rups')).getText();
59+
expect(runnersUp).toContain('RubberMan, Mr. Nice');
60+
});
61+
62+
it('should render DateLogger log entry in Hero-of-the-Month', function () {
63+
var logs = element.all(by.id('logs')).get(0).getText();
64+
expect(logs).toContain('INFO: starting up at');
65+
});
66+
67+
it('should highlight Hero Bios and Contacts container when mouseover', function () {
68+
var target = element(by.css('div[myHighlight="yellow"]'))
69+
var yellow = "rgba(255, 255, 0, 1)";
70+
71+
expect(target.getCssValue('background-color')).not.toEqual(yellow);
72+
browser.actions().mouseMove(target).perform();
73+
expect(target.getCssValue('background-color')).toEqual(yellow);
74+
});
75+
76+
describe('in Parent Finder', function () {
77+
var cathy1 = element(by.css('alex cathy'));
78+
var craig1 = element(by.css('alex craig'));
79+
var carol1 = element(by.css('alex carol p'));
80+
var carol2 = element(by.css('barry carol p'));
81+
82+
it('"Cathy" should find "Alex" via the component class', function () {
83+
expect(cathy1.getText()).toContain('Found Alex via the component');
84+
});
85+
86+
it('"Craig" should not find "Alex" via the base class', function () {
87+
expect(craig1.getText()).toContain('Did not find Alex via the base');
88+
});
89+
90+
it('"Carol" within "Alex" should have "Alex" parent', function () {
91+
expect(carol1.getText()).toContain('Alex');
92+
});
93+
94+
it('"Carol" within "Barry" should have "Barry" parent', function () {
95+
expect(carol2.getText()).toContain('Barry');
96+
});
97+
})
98+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.js
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<h1>DI Cookbook</h1>
2+
<div class="di-component">
3+
<h3>Logged in user</h3>
4+
<div>Name: {{userContext.name}}</div>
5+
<div>Role: {{userContext.role}}</div>
6+
</div>
7+
8+
<div class="di-component">
9+
<h3>Hero Bios</h3>
10+
<hero-bios></hero-bios>
11+
</div>
12+
13+
<!-- #docregion highlight -->
14+
<div id="highlight" class="di-component" myHighlight>
15+
<h3>Hero Bios and Contacts</h3>
16+
<div myHighlight="yellow">
17+
<hero-bios-and-contacts></hero-bios-and-contacts>
18+
</div>
19+
</div>
20+
<!-- #enddocregion highlight -->
21+
22+
<div class="di-component">
23+
<hero-of-the-month></hero-of-the-month>
24+
</div>
25+
26+
27+
<div class="di-component">
28+
<h3>Unsorted Heroes</h3>
29+
<unsorted-heroes></unsorted-heroes>
30+
</div>
31+
32+
<div class="di-component">
33+
<h3>Sorted Heroes</h3>
34+
<sorted-heroes></sorted-heroes>
35+
</div>
36+
37+
<div class="di-component">
38+
<parent-finder></parent-finder>
39+
</div>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// #docregion
2+
import { Component } from 'angular2/core';
3+
4+
import { HeroBiosComponent,
5+
HeroBiosAndContactsComponent} from './hero-bios.component';
6+
import { HeroOfTheMonthComponent } from './hero-of-the-month.component';
7+
import { HeroesBaseComponent,
8+
SortedHeroesComponent } from './sorted-heroes.component';
9+
import { HighlightDirective } from './highlight.directive';
10+
import { ParentFinderComponent } from './parent-finder.component';
11+
12+
const DIRECTIVES = [
13+
HeroBiosComponent, HeroBiosAndContactsComponent,
14+
HeroesBaseComponent, SortedHeroesComponent,
15+
HeroOfTheMonthComponent,
16+
HighlightDirective,
17+
ParentFinderComponent
18+
];
19+
20+
// #docregion import-services
21+
import { LoggerService } from './logger.service';
22+
import { UserContextService } from './user-context.service';
23+
import { UserService } from './user.service';
24+
25+
@Component({
26+
selector: 'my-app',
27+
templateUrl:'app/app.component.html',
28+
directives: DIRECTIVES,
29+
// #docregion providers
30+
providers: [LoggerService, UserContextService, UserService]
31+
// #enddocregion providers
32+
})
33+
export class AppComponent {
34+
// #enddocregion import-services
35+
36+
private userId:number = 1;
37+
38+
// #docregion ctor
39+
constructor(logger:LoggerService, public userContext:UserContextService) {
40+
userContext.loadUser(this.userId);
41+
logger.logInfo('AppComponent initialized');
42+
}
43+
// #enddocregion ctor
44+
// #docregion import-services
45+
}
46+
// #enddocregion import-services
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* tslint:disable:one-line:check-open-brace*/
2+
// #docregion
3+
import { Injectable } from 'angular2/core';
4+
import { LoggerService } from './logger.service';
5+
6+
// #docregion minimal-logger
7+
// class used as a restricting interface (hides other public members)
8+
export abstract class MinimalLogger {
9+
logInfo: (msg: string) => void;
10+
logs: string[];
11+
}
12+
// #enddocregion minimal-logger
13+
14+
/*
15+
// Transpiles to:
16+
// #docregion minimal-logger-transpiled
17+
var MinimalLogger = (function () {
18+
function MinimalLogger() {}
19+
return MinimalLogger;
20+
}());
21+
exports("MinimalLogger", MinimalLogger);
22+
// #enddocregion minimal-logger-transpiled
23+
*/
24+
25+
// #docregion date-logger-service
26+
@Injectable()
27+
// #docregion date-logger-service-signature
28+
export class DateLoggerService extends LoggerService implements MinimalLogger
29+
// #enddocregion date-logger-service-signature
30+
{
31+
logInfo(msg: any) { super.logInfo(stamp(msg)); }
32+
logDebug(msg: any) { super.logInfo(stamp(msg)); }
33+
logError(msg: any) { super.logError(stamp(msg)); }
34+
}
35+
36+
function stamp(msg: any) { return msg + ' at ' + new Date(); }
37+
// #enddocregion date-logger-service
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #docregion
2+
import {Component, Input, OnInit} from 'angular2/core';
3+
4+
import {Hero} from './hero';
5+
import {HeroCacheService} from './hero-cache.service';
6+
7+
// #docregion component
8+
@Component({
9+
selector:'hero-bio',
10+
// #docregion template
11+
template:`
12+
<h4>{{hero.name}}</h4>
13+
<ng-content></ng-content>
14+
<textarea cols="25" [(ngModel)]="hero.description"></textarea>`,
15+
// #enddocregion template
16+
providers: [HeroCacheService]
17+
})
18+
19+
export class HeroBioComponent implements OnInit {
20+
21+
@Input() heroId:number;
22+
23+
constructor(private _heroCache:HeroCacheService) { }
24+
25+
ngOnInit() { this._heroCache.fetchCachedHero(this.heroId); }
26+
27+
get hero() { return this._heroCache.hero; }
28+
}
29+
// #enddocregion component
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// #docplaster
2+
// #docregion
3+
import { Component} from 'angular2/core';
4+
5+
import { HeroContactComponent } from './hero-contact.component';
6+
import { HeroBioComponent } from './hero-bio.component';
7+
import { HeroService } from './hero.service';
8+
import { LoggerService } from './logger.service';
9+
10+
//////// HeroBiosComponent ////
11+
// #docregion simple
12+
@Component({
13+
selector:'hero-bios',
14+
template:`
15+
<hero-bio [heroId]="1"></hero-bio>
16+
<hero-bio [heroId]="2"></hero-bio>
17+
<hero-bio [heroId]="3"></hero-bio>`,
18+
directives:[HeroBioComponent],
19+
providers: [HeroService]
20+
})
21+
export class HeroBiosComponent{
22+
// #enddocregion simple
23+
// #docregion ctor
24+
constructor(logger: LoggerService) {
25+
logger.logInfo('Creating HeroBiosComponent');
26+
}
27+
// #enddocregion ctor
28+
// #docregion simple
29+
}
30+
// #enddocregion simple
31+
32+
//////// HeroBiosAndContactsComponent ////
33+
// #docregion hero-bios-and-contacts
34+
@Component({
35+
selector:'hero-bios-and-contacts',
36+
// #docregion template
37+
template:`
38+
<hero-bio [heroId]="1"> <hero-contact></hero-contact> </hero-bio>
39+
<hero-bio [heroId]="2"> <hero-contact></hero-contact> </hero-bio>
40+
<hero-bio [heroId]="3"> <hero-contact></hero-contact> </hero-bio>`,
41+
// #enddocregion template
42+
directives:[HeroBioComponent, HeroContactComponent],
43+
// #docregion class-provider
44+
providers: [HeroService]
45+
// #enddocregion class-provider
46+
})
47+
export class HeroBiosAndContactsComponent{
48+
constructor(logger: LoggerService) {
49+
logger.logInfo('Creating HeroBiosAndContactsComponent');
50+
}
51+
}
52+
// #enddocregion hero-bios-and-contacts

0 commit comments

Comments
 (0)