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

Commit 4dffc41

Browse files
cburgdorfwardbell
authored andcommitted
docs(hierarchical-injectors): use real example code
closes #406 With this change the chapter doesn't use inline code anymore but instead uses proper runnable example code.
1 parent 6be22a0 commit 4dffc41

File tree

12 files changed

+237
-159
lines changed

12 files changed

+237
-159
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/**/*.js
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "angular2-getting-started",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"tsc": "tsc -p src -w",
8+
"start": "live-server --open=src"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"angular2": "2.0.0-alpha.46",
15+
"systemjs": "0.19.2"
16+
},
17+
"devDependencies": {
18+
"live-server": "^0.8.1",
19+
"typescript": "^1.6.2"
20+
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// #docregion
2+
export class EditItem<T> {
3+
editing: boolean
4+
constructor (public item: T) {}
5+
}
6+
// #docregion
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion
2+
import {Component, Input} from 'angular2/angular2';
3+
import {Hero} from './hero';
4+
5+
@Component({
6+
selector: 'hero-card',
7+
template: `
8+
<div>
9+
<span>Name:</span>
10+
<span>{{hero.name}}</span>
11+
</div>`
12+
})
13+
export class HeroCardComponent {
14+
@Input() hero: Hero;
15+
}
16+
// #docregion
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// #docregion
2+
import {Component, Input, Output, EventEmitter} from 'angular2/angular2';
3+
import {RestoreService} from './restore-service';
4+
import {Hero} from './hero';
5+
6+
@Component({
7+
selector: 'hero-editor',
8+
// #docregion providers
9+
providers: [RestoreService],
10+
// #enddocregion providers
11+
template: `
12+
<div>
13+
<span>Name:</span>
14+
<input [(ng-model)]="hero.name"/>
15+
<div>
16+
<button (click)="onSaved()">save</button>
17+
<button (click)="onCanceled()">cancel</button>
18+
</div>
19+
</div>`
20+
})
21+
22+
export class HeroEditorComponent {
23+
@Output() canceled = new EventEmitter();
24+
@Output() saved = new EventEmitter();
25+
26+
constructor(private restoreService: RestoreService<Hero>) {}
27+
28+
@Input()
29+
set hero (hero: Hero) {
30+
this.restoreService.setItem(hero);
31+
}
32+
33+
get hero () {
34+
return this.restoreService.getItem();
35+
}
36+
37+
onSaved () {
38+
this.saved.next(this.restoreService.getItem());
39+
}
40+
41+
onCanceled () {
42+
this.hero = this.restoreService.restoreItem();
43+
this.canceled.next(this.hero);
44+
}
45+
}
46+
// #enddocregion
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// #docregion
2+
export class Hero {
3+
name: string;
4+
power: string;
5+
}
6+
// #enddocregion
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// #docregion
2+
import {Component, bootstrap} from 'angular2/angular2';
3+
import {EditItem} from './edit-item';
4+
import {HeroesService} from './heroes-service';
5+
import {HeroCardComponent} from './hero-card.component';
6+
import {HeroEditorComponent} from './hero-editor.component';
7+
import {Hero} from './hero';
8+
9+
@Component({
10+
selector: 'heroes-list',
11+
template: `
12+
<div>
13+
<ul>
14+
<li *ng-for="#editItem of heroes">
15+
<hero-card
16+
[hidden]="editItem.editing"
17+
[hero]="editItem.item">
18+
</hero-card>
19+
<button
20+
[hidden]="editItem.editing"
21+
(click)="editItem.editing = true">
22+
edit
23+
</button>
24+
<hero-editor
25+
(saved)="onSaved(editItem, $event)"
26+
(canceled)="onCanceled(editItem)"
27+
[hidden]="!editItem.editing"
28+
[hero]="editItem.item">
29+
</hero-editor>
30+
</li>
31+
</ul>
32+
</div>`,
33+
directives: [HeroCardComponent, HeroEditorComponent]
34+
})
35+
export class HeroesListComponent {
36+
heroes: Array<EditItem<Hero>>;
37+
constructor(heroesService: HeroesService) {
38+
this.heroes = heroesService.getHeroes()
39+
.map(item => new EditItem(item));
40+
}
41+
42+
onSaved (editItem: EditItem<Hero>, updatedHero: Hero) {
43+
editItem.item = updatedHero;
44+
editItem.editing = false;
45+
}
46+
47+
onCanceled (editItem: EditItem<Hero>) {
48+
editItem.editing = false;
49+
}
50+
}
51+
52+
bootstrap(HeroesListComponent, [HeroesService]);
53+
// #enddocregion
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Hero} from './hero';
2+
3+
export class HeroesService {
4+
heroes: Array<Hero> = [
5+
{ name: "RubberMan", power: 'flexibility'},
6+
{ name: "Tornado", power: 'Weather changer'}
7+
];
8+
9+
getHeroes () {
10+
return this.heroes;
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// #docregion
2+
export class RestoreService<T> {
3+
originalItem: T;
4+
currentItem: T;
5+
6+
setItem (item: T) {
7+
this.originalItem = item;
8+
this.currentItem = this.clone(item);
9+
}
10+
11+
getItem () :T {
12+
return this.currentItem;
13+
}
14+
15+
restoreItem () :T {
16+
this.currentItem = this.originalItem;
17+
return this.getItem();
18+
}
19+
20+
clone (item: T) :T {
21+
// super poor clone implementation
22+
return JSON.parse(JSON.stringify(item));
23+
}
24+
}
25+
// #enddocregion
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Hierarchical Injectors</title>
6+
<script src="../node_modules/systemjs/dist/system.src.js"></script>
7+
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
8+
<script>
9+
System.config({
10+
packages: {'app': {defaultExtension: 'js'}}
11+
});
12+
System.import('app/heroes-list.component');
13+
</script>
14+
</head>
15+
16+
<body>
17+
<!-- #docregion heroes-list -->
18+
<heroes-list>loading...</heroes-list>
19+
<!-- #enddocregion heroes-list -->
20+
</body>
21+
22+
</html>

0 commit comments

Comments
 (0)