This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 875
docs(guide): add dart version of hierarchical dependency injection #683
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
public/docs/_examples/hierarchical-dependency-injection/dart/lib/edit_item.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// #docregion | ||
class EditItem<T> { | ||
bool editing = false; | ||
T item; | ||
EditItem(this.item); | ||
} | ||
// #enddocregion |
14 changes: 14 additions & 0 deletions
14
public/docs/_examples/hierarchical-dependency-injection/dart/lib/hero.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
|
||
class Hero { | ||
String name; | ||
String power; | ||
|
||
Hero clone() { | ||
return new Hero() | ||
..name = name | ||
..power = power; | ||
} | ||
} | ||
// #enddocregion |
16 changes: 16 additions & 0 deletions
16
public/docs/_examples/hierarchical-dependency-injection/dart/lib/hero_card_component.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
import 'package:hierarchical_di/hero.dart'; | ||
|
||
@Component( | ||
selector: 'hero-card', | ||
template: ''' | ||
<div> | ||
<span>Name:</span> | ||
<span>{{hero.name}}</span> | ||
</div> | ||
''') | ||
class HeroCardComponent { | ||
@Input() Hero hero; | ||
} | ||
// #docregion |
47 changes: 47 additions & 0 deletions
47
public/docs/_examples/hierarchical-dependency-injection/dart/lib/hero_editor_component.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
import 'package:hierarchical_di/restore_service.dart'; | ||
import 'package:hierarchical_di/hero.dart'; | ||
|
||
@Component( | ||
selector: 'hero-editor', | ||
// #docregion providers | ||
providers: const [RestoreService], | ||
// #enddocregion providers | ||
template: ''' | ||
<div> | ||
<span>Name:</span> | ||
<input [(ngModel)]="hero.name"/> | ||
<div> | ||
<button (click)="onSaved()">save</button> | ||
<button (click)="onCanceled()">cancel</button> | ||
</div> | ||
</div> | ||
''') | ||
class HeroEditorComponent { | ||
@Output() final EventEmitter canceled = new EventEmitter(); | ||
@Output() final EventEmitter saved = new EventEmitter(); | ||
|
||
RestoreService<Hero> _restoreService; | ||
|
||
HeroEditorComponent(this._restoreService); | ||
|
||
@Input() | ||
set hero(Hero hero) { | ||
_restoreService.setItem(hero); | ||
} | ||
|
||
Hero get hero { | ||
return _restoreService.getItem(); | ||
} | ||
|
||
onSaved() { | ||
saved.add(_restoreService.getItem()); | ||
} | ||
|
||
onCanceled() { | ||
hero = _restoreService.restoreItem(); | ||
canceled.add(hero); | ||
} | ||
} | ||
// #enddocregion |
53 changes: 53 additions & 0 deletions
53
public/docs/_examples/hierarchical-dependency-injection/dart/lib/heroes_list_component.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
import 'package:hierarchical_di/hero.dart'; | ||
import 'package:hierarchical_di/heroes_service.dart'; | ||
import 'package:hierarchical_di/hero_editor_component.dart'; | ||
import 'package:hierarchical_di/hero_card_component.dart'; | ||
import 'package:hierarchical_di/edit_item.dart'; | ||
|
||
@Component( | ||
selector: 'heroes-list', | ||
template: ''' | ||
<div> | ||
<ul> | ||
<li *ngFor="#editItem of heroes"> | ||
<hero-card | ||
[hidden]="editItem.editing" | ||
[hero]="editItem.item"> | ||
</hero-card> | ||
<button | ||
[hidden]="editItem.editing" | ||
(click)="editItem.editing = true"> | ||
edit | ||
</button> | ||
<hero-editor | ||
(saved)="onSaved(editItem, \$event)" | ||
(canceled)="onCanceled(editItem)" | ||
[hidden]="!editItem.editing" | ||
[hero]="editItem.item"> | ||
</hero-editor> | ||
</li> | ||
</ul> | ||
</div> | ||
''', | ||
directives: const [HeroCardComponent, HeroEditorComponent]) | ||
class HeroesListComponent { | ||
List<EditItem<Hero>> heroes; | ||
HeroesListComponent(HeroesService heroesService) { | ||
heroes = heroesService | ||
.getHeroes() | ||
.map((Hero item) => new EditItem(item)) | ||
.toList(); | ||
} | ||
|
||
onSaved(EditItem<Hero> editItem, Hero updatedHero) { | ||
editItem.item = updatedHero; | ||
editItem.editing = false; | ||
} | ||
|
||
onCanceled(EditItem<Hero> editItem) { | ||
editItem.editing = false; | ||
} | ||
} | ||
// #enddocregion |
18 changes: 18 additions & 0 deletions
18
public/docs/_examples/hierarchical-dependency-injection/dart/lib/heroes_service.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:angular2/angular2.dart'; | ||
import 'package:hierarchical_di/hero.dart'; | ||
|
||
@Injectable() | ||
class HeroesService { | ||
List<Hero> _heroes = [ | ||
new Hero() | ||
..name = "RubberMan" | ||
..power = 'Flexibility', | ||
new Hero() | ||
..name = "Tornado" | ||
..power = 'Weather changer' | ||
]; | ||
|
||
List<Hero> getHeroes() { | ||
return _heroes; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
public/docs/_examples/hierarchical-dependency-injection/dart/lib/restore_service.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// #docregion | ||
import 'package:angular2/angular2.dart'; | ||
|
||
@Injectable() | ||
class RestoreService<T> { | ||
T _originalItem; | ||
T _currentItem; | ||
|
||
setItem(T item) { | ||
print(item.runtimeType); | ||
_originalItem = item; | ||
_currentItem = clone(item); | ||
} | ||
|
||
T getItem() { | ||
return _currentItem; | ||
} | ||
|
||
T restoreItem() { | ||
_currentItem = _originalItem; | ||
return getItem(); | ||
} | ||
|
||
T clone(T item) { | ||
// super poor clone implementation | ||
return item.clone(); | ||
} | ||
} | ||
// #enddocregion |
19 changes: 19 additions & 0 deletions
19
public/docs/_examples/hierarchical-dependency-injection/dart/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# #docregion | ||
name: 'hierarchical_di' | ||
version: 0.0.1 | ||
description: hierarchical dependency injection example | ||
|
||
environment: | ||
sdk: '>=1.0.0 <2.0.0' | ||
|
||
dependencies: | ||
angular2: '2.0.0-beta.1' | ||
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 didn't realize we were on beta.1! |
||
browser: ^0.10.0 | ||
dart_to_js_script_rewriter: '^0.1.0' | ||
|
||
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. browser? |
||
transformers: | ||
- angular2: | ||
platform_directives: | ||
- 'package:angular2/common.dart#COMMON_DIRECTIVES' | ||
entry_points: web/main.dart | ||
- dart_to_js_script_rewriter |
19 changes: 19 additions & 0 deletions
19
public/docs/_examples/hierarchical-dependency-injection/dart/web/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Hierarchical Injector</title> | ||
<script defer src="main.dart" type="application/dart"></script> | ||
<script defer src="packages/browser/dart.js"></script> | ||
</head> | ||
|
||
<body> | ||
<!-- #docregion heroes-list --> | ||
<heroes-list>loading...</heroes-list> | ||
<!-- #enddocregion heroes-list --> | ||
|
||
</body> | ||
</html> |
15 changes: 15 additions & 0 deletions
15
public/docs/_examples/hierarchical-dependency-injection/dart/web/main.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// #docregion | ||
import 'package:angular2/bootstrap.dart'; | ||
import 'package:hierarchical_di/heroes_service.dart'; | ||
import 'package:hierarchical_di/heroes_list_component.dart'; | ||
|
||
void main() { | ||
bootstrap(HeroesListComponent, [HeroesService]); | ||
} | ||
|
||
/* Documentation artifact below | ||
// #docregion bad-alternative | ||
// Don't do this! | ||
bootstrap(HeroesListComponent, [HeroesService, RestoreService]) | ||
// #enddocregion bad-alternative | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this one can be final
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.
Also private. Good catch +1
Edit: nope it must be public.