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

Commit 58113ec

Browse files
committed
convert toh tutorial for dart (except toh-pt5)
1 parent b20a451 commit 58113ec

File tree

5 files changed

+257
-254
lines changed

5 files changed

+257
-254
lines changed

public/docs/dart/latest/tutorial/index.jade

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ include ../../../../_includes/_util-fns
2424
We'll be covering a lot of ground at an introductory level but we’ll find plenty of links
2525
to chapters with greater depth.
2626

27-
[Run the live example](/resources/live-examples/tutorial/ts/plnkr.html)
27+
<!--
28+
TODO(kasperpeulen): Make a github repo with gh-pages for this:
29+
[Run the live example](/resources/live-examples/tutorial/ts/plnkr.html)
30+
-->
2831

2932
.l-main-section
3033
:marked

public/docs/dart/latest/tutorial/toh-pt1.jade

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,41 @@ include ../../../../_includes/_util-fns
55

66
Every story starts somewhere. Our story starts where the [QuickStart](../quickstart.html) ends.
77

8-
[Run the live example for part 1](/resources/live-examples/toh-1/ts/plnkr.html)
8+
<!--
9+
TODO(kasperpeulen): Make a github repo with gh-pages for this:
10+
[Run the live example for part 1](/resources/live-examples/toh-1/ts/plnkr.html)
11+
-->
912

1013
Follow the "QuickStart" steps. They provide the prerequisites, the folder structure,
1114
and the core files for our Tour of Heroes.
1215

13-
Copy the "QuickStart" code to a new folder and rename the folder `angular2-tour-of-heroes`.
16+
<!--
17+
TODO(kasperpeulen): Recommand stagehand for this
18+
-->
19+
20+
Copy the "QuickStart" code to a new folder and rename the folder `angular2_tour_of_heroes`.
1421
We should have the following structure:
1522

1623
.filetree
17-
.file angular2-tour-of-heroes
24+
.file angular2_tour_of_heroes
1825
.children
19-
.file node_modules
20-
.file app
26+
.file lib
27+
.children
28+
.file app_component.dart
29+
.file web
2130
.children
22-
.file app.component.ts
23-
.file boot.ts
24-
.file index.html
25-
.file package.json
26-
.file tsconfig.json
31+
.file index.html
32+
.file main.dart
33+
.file pubspec.yaml
2734
:marked
2835
## Keep the app transpiling and running
29-
We want to start the TypeScript compiler, have it watch for changes, and start our server. We'll do this by typing
36+
We want to start the Dart compiler, have it watch for changes, and start our server. We'll do this by typing
3037

3138
code-example(format="" language="bash").
32-
npm start
39+
pub serve
3340

3441
:marked
35-
This command runs the compiler in watch mode, starts the server, launches the app in a browser,
42+
This command runs the compiler in watch mode, starts the server,
3643
and keeps the app running while we continue to build the Tour of Heroes.
3744

3845
.l-main-section
@@ -43,12 +50,12 @@ code-example(format="" language="bash").
4350
Let's add two properties to our `AppComponent`, a `title` property for the application name and a `hero` property
4451
for a hero named "Windstorm".
4552

46-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'app-component-1', 'app.component.ts (AppComponent class)')(format=".")
53+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'app-component-1', 'app_component.dart (AppComponent class)')(format=".")
4754

4855
:marked
4956
Now we update the template in the `@Component` decoration with data bindings to these new properties.
5057

51-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'show-hero')
58+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'show-hero')
5259

5360
:marked
5461
The browser should refresh and display our title and hero.
@@ -62,48 +69,32 @@ code-example(format="" language="bash").
6269
### Hero object
6370

6471
At the moment, our hero is just a name. Our hero needs more properties.
65-
Let's convert the `hero` from a literal string to an interface.
66-
67-
Create a `Hero` interface with `id` and `name` properties.
68-
Keep this near the top of the `app.component.ts` file for now.
72+
Let's convert the `hero` from a literal string to a class.
6973

70-
+makeExample('toh-1/ts/app/app.component.ts', 'hero-interface-1', 'app.component.ts (Hero interface)')(format=".")
74+
Create a `Hero` class with `id` and `name` properties.
75+
Keep this near the top of the `app_component.dart` file for now.
7176

72-
.l-sub-section
73-
:marked
74-
#### Interface or Class?
75-
Why a `Hero` interface and not a `Hero` class?
76-
We want a strongly typed `Hero`. We get strong typing with either option.
77-
Our choice depends on how we intend to use the `Hero`.
78-
79-
If we need a `Hero` that goes beyond simple properties, a `Hero` with logic and behavior,
80-
we must define a class.
81-
If we only need type checking, the interface is sufficient and lighter weight.
82-
83-
Lighter weight? Transpiling a class to JavaScript produces code.
84-
Transpiling an interface produces &mdash; nothing.
85-
If the class does nothing (and there is nothing for a `Hero` class to do right now),
86-
we prefer an interface.
77+
+makeExample('toh-1/dart/lib/app_component.dart', 'hero-class-1', 'app_component.dart (Hero class)')(format=".")
8778

8879
:marked
89-
Now that we have a `Hero` interface, let’s refactor our component’s `hero` property to be of type `Hero`.
80+
Now that we have a `Hero` class, let’s refactor our component’s `hero` property to be of type `Hero`.
9081
Then initialize it with an id of `1` and the name, "Windstorm".
9182

92-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'hero-property-1', 'app.component.ts (Hero property)')(format=".")
83+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'hero-property-1', 'app_component.dart (Hero property)')(format=".")
9384

9485
:marked
9586
Because we changed the hero from a string to an object,
9687
we update the binding in the template to refer to the hero’s `name` property.
9788

98-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'show-hero-2')
89+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'show-hero-2')
9990
:marked
10091
The browser refreshes and continues to display our hero’s name.
10192

10293
### Adding more HTML
10394
Displaying a name is good, but we want to see all of our hero’s properties.
10495
We’ll add a `<div>` for our hero’s `id` property and another `<div>` for our hero’s `name`.
10596

106-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'show-hero-properties')
97+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'show-hero-properties')
10798
:marked
10899
Uh oh, our template string is getting long. We better take care of that to avoid the risk of making a typo in the template.
109100

@@ -113,12 +104,12 @@ code-example(format="" language="bash").
113104
but that gets ugly fast, it is harder to read, and
114105
it is easy to make a spelling error. Instead,
115106
let’s take advantage of the template strings feature
116-
in ES2015 and TypeScript to maintain our sanity.
107+
in Dart to maintain our sanity.
117108

118-
Change the quotes around the template to back-ticks and
109+
Change the quotes around the template to triple quotes and
119110
put the `<h1>`, `<h2>` and `<div>` elements on their own lines.
120111

121-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'multi-line-strings', 'app.component.ts (AppComponent\'s template)')
112+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'multi-line-strings', 'app_component.dart (AppComponent\'s template)')
122113

123114
.callout.is-important
124115
header A back-tick is not a single quote
@@ -138,7 +129,7 @@ code-example(format="" language="bash").
138129

139130
Refactor the hero name `<label>` with `<label>` and `<input>` elements as shown below:
140131

141-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'editing-Hero', 'app.component.ts (input element)')
132+
+makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'editing-Hero', 'app_component.dart (input element)')
142133
:marked
143134
We see in the browser that the hero’s name does appear in the `<input>` textbox.
144135
But something doesn’t feel right.
@@ -176,16 +167,19 @@ code-example(language="html").
176167

177168
* Our Tour of Heroes uses the double curly braces of interpolation (a form of one-way data binding)
178169
to display the application title and properties of a `Hero` object.
179-
* We wrote a multi-line template using ES2015’s template strings to make our template readable.
170+
* We wrote a multi-line template using Dart's template strings to make our template readable.
180171
* We can both display and change the hero’s name after adding a two-way data binding to the `<input>` element
181172
using the built-in `ngModel` directive.
182173
* The `ngModel` directive also propagates changes to every other binding of the `hero.name`.
183174

184-
[Run the live example for part 1](/resources/live-examples/toh-1/ts/plnkr.html)
175+
<!--
176+
TODO(kasperpeulen): Make a github repo with gh-pages for this:
177+
[Run the live example for part 1](/resources/live-examples/toh-1/ts/plnkr.html)
178+
-->
185179

186-
Here's the complete `app.component.ts` as it stands now:
180+
Here's the complete `app_component.dart` as it stands now:
187181

188-
+makeExample('toh-1/ts/app/app.component.ts', 'pt1', 'app.component.ts')
182+
+makeExample('toh-1/dart/lib/app_component.dart', 'pt1', 'app_component.dart')
189183

190184
.l-main-section
191185
:marked

public/docs/dart/latest/tutorial/toh-pt2.jade

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ include ../../../../_includes/_util-fns
66
We’ll expand our Tour of Heroes app to display a list of heroes,
77
allow the user to select a hero, and display the hero’s details.
88

9-
[Run the live example for part 2](/resources/live-examples/toh-2/ts/plnkr.html)
9+
<!--
10+
TODO(kasperpeulen): Make a github repo with gh-pages for this:
11+
[Run the live example for part 2](/resources/live-examples/toh-2/dart/plnkr.html)
12+
-->
1013

1114
Let’s take stock of what we’ll need to display a list of heroes.
1215
First, we need a list of heroes. We want to display those heroes in the view’s template,
@@ -20,22 +23,22 @@ include ../../../../_includes/_util-fns
2023
If not, we’ll need to go back to Part 1 and figure out what we missed.
2124

2225
.filetree
23-
.file angular2-tour-of-heroes
26+
.file angular2_tour_of_heroes
2427
.children
25-
.file node_modules
26-
.file app
28+
.file lib
2729
.children
28-
.file app.component.ts
29-
.file boot.ts
30-
.file index.html
31-
.file package.json
32-
.file tsconfig.json
30+
.file app_component.dart
31+
.file web
32+
.children
33+
.file index.html
34+
.file main.dart
35+
.file pubspec.yaml
3336
:marked
3437
### Keep the app transpiling and running
35-
We want to start the TypeScript compiler, have it watch for changes, and start our server. We'll do this by typing
38+
We want to start the Dart compiler, have it watch for changes, and start our server. We'll do this by typing
3639

3740
code-example(format="." language="bash").
38-
npm start
41+
pub serve
3942

4043
:marked
4144
This will keep the application running while we continue to build the Tour of Heroes.
@@ -44,23 +47,21 @@ code-example(format="." language="bash").
4447
:marked
4548
## Displaying Our Heroes
4649
### Creating heroes
47-
Let’s create an array of ten heroes at the bottom of `app.component.ts`.
50+
Let’s create an array of ten heroes at the bottom of `app_component.dart`.
4851

49-
+makeExample('toh-2/ts/app/app.component.ts', 'hero-array', 'app.component.ts (Hero array)')
52+
+makeExample('toh-2/dart/lib/app_component.dart', 'hero-array', 'app_component.dart (Hero list)')
5053

5154
:marked
52-
The `HEROES` array is of type `Hero`, the interface defined in part one,
53-
to create an array of heroes.
55+
The `mockHeroes` list is of type `Hero`, the class defined in part one,
56+
to create an list of heroes.
5457
We aspire to fetch this list of heroes from a web service, but let’s take small steps
5558
first and display mock heroes.
5659

5760
### Exposing heroes
5861
Let’s create a public property in `AppComponent` that exposes the heroes for binding.
5962

60-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'hero-array-1', 'app.component.ts (Hero array property)')
63+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'hero-array-1', 'app_component.dart (Hero array property)')
6164

62-
:marked
63-
We did not have to define the `heroes` type. TypeScript can infer it from the `HEROES` array.
6465
.l-sub-section
6566
:marked
6667
We could have defined the heroes list here in this component class.
@@ -72,7 +73,7 @@ code-example(format="." language="bash").
7273
Our component has `heroes`. Let’s create an unordered list in our template to display them.
7374
We’ll insert the following chunk of HTML below the title and above the hero details.
7475

75-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'heroes-template-1', 'app.component.ts (Heroes template)')
76+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'heroes-template-1', 'app_component.dart (Heroes template)')
7677

7778
:marked
7879
Now we have a template that we can fill with our heroes.
@@ -85,7 +86,7 @@ code-example(format="." language="bash").
8586

8687
First modify the `<li>` tag by adding the built-in directive `*ngFor`.
8788

88-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'heroes-ngfor-1', 'app.component.ts (ngFor)')
89+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'heroes-ngfor-1', 'app_component.dart (ngFor)')
8990

9091
.alert.is-critical
9192
:marked
@@ -114,7 +115,7 @@ code-example(format="." language="bash").
114115
Now we insert some content between the `<li>` tags
115116
that uses the `hero` template variable to display the hero’s properties.
116117

117-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'ng-for', 'app.component.ts (ngFor template)')(format=".")
118+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'ng-for', 'app_component.dart (ngFor template)')(format=".")
118119

119120
:marked
120121
When the browser refreshes, we see a list of heroes!
@@ -126,7 +127,7 @@ code-example(format="." language="bash").
126127
Let’s add some styles to our component by setting the `styles` property on the `@Component` decorator
127128
to the following CSS classes:
128129

129-
+makeExample('toh-2/ts/app/app.component.ts', 'styles-1', 'app.component.ts (Styling)')
130+
+makeExample('toh-2/dart/lib/app_component.dart', 'styles-1', 'app_component.dart (Styling)')
130131

131132
:marked
132133
Notice that we again use the back-tick notation for multi-line strings.
@@ -136,7 +137,7 @@ code-example(format="." language="bash").
136137

137138
Our template for displaying the heroes should now look like this:
138139

139-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'heroes-styled', 'app.component.ts (Styled heroes)')
140+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'heroes-styled', 'app_component.dart (Styled heroes)')
140141

141142
:marked
142143
That's a lot of styles! We can put them inline as shown here, or we can move them out to their own file which will make it easier to code our component.
@@ -156,7 +157,7 @@ code-example(format="." language="bash").
156157
### Click event
157158
We modify the `<li>` by inserting an Angular event binding to its click event.
158159

159-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'selectedHero-click', 'app.component.ts (Capturing the click event)')
160+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'selectedHero-click', 'app_component.dart (Capturing the click event)')
160161

161162
:marked
162163
Focus on the event binding
@@ -185,21 +186,21 @@ code-example(format="." language="bash").
185186
We no longer need the static `hero` property of the `AppComponent`.
186187
**Replace** it with this simple `selectedHero` property:
187188

188-
+makeExample('toh-2/ts/app/app.component.ts', 'selected-hero-1', 'app.component.ts (selectedHero)')
189+
+makeExample('toh-2/dart/lib/app_component.dart', 'selected-hero-1', 'app_component.dart (selectedHero)')
189190

190191
:marked
191192
We’ve decided that none of the heroes should be selected before the user picks a hero so
192193
we won’t initialize the `selectedHero` as we were doing with `hero`.
193194

194195
Now **add an `onSelect` method** that sets the `selectedHero` property to the `hero` the user clicked.
195-
+makeExample('toh-2/ts/app/app.component.ts', 'on-select-1', 'app.component.ts (onSelect)')
196+
+makeExample('toh-2/dart/lib/app_component.dart', 'on-select-1', 'app_component.dart (onSelect)')
196197

197198
:marked
198199
We will be showing the selected hero's details in our template.
199200
At the moment, it is still referring to the old `hero` property.
200201
Let’s fix the template to bind to the new `selectedHero` property.
201202

202-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'selectedHero-details', 'app.component.ts (Binding to the selectedHero\'s name)')
203+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'selectedHero-details', 'app_component.dart (Binding to the selectedHero\'s name)')
203204
:marked
204205
### Hide the empty detail with ngIf
205206

@@ -219,7 +220,7 @@ code-example(format="." language="bash").
219220
We wrap the HTML hero detail content of our template with a `<div>`.
220221
Then we add the `ngIf` built-in directive and set it to the `selectedHero` property of our component.
221222

222-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'ng-if', 'app.component.ts (ngIf)')
223+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'ng-if', 'app_component.dart (ngIf)')
223224

224225
.alert.is-critical
225226
:marked
@@ -229,7 +230,7 @@ code-example(format="." language="bash").
229230
When there is no `selectedHero`, the `ngIf` directive removes the hero detail HTML from the DOM.
230231
There will be no hero detail elements and no bindings to worry about.
231232

232-
When the user picks a hero, `selectedHero` becomes "truthy" and
233+
When the user picks a hero, `selectedHero` isn't `null` anymore and
233234
`ngIf` puts the hero detail content into the DOM and evaluates the nested bindings.
234235
.l-sub-section
235236
:marked
@@ -261,12 +262,12 @@ code-example(format="." language="bash").
261262

262263
The key is the name of the CSS class (`selected`). The value is `true` if the two heroes match and `false` otherwise.
263264
We’re saying “*apply the `selected` class if the heroes match, remove it if they don’t*”.
264-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'class-selected-1', 'app.component.ts (Setting the CSS class)')(format=".")
265+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'class-selected-1', 'app_component.dart (Setting the CSS class)')(format=".")
265266
:marked
266267
Notice in the template that the `class.selected` is surrounded in square brackets (`[]`).
267268
This is the syntax for a Property Binding, a binding in which data flows one way
268269
from the data source (the expression `hero === selectedHero`) to a property of `class`.
269-
+makeExample('toh-2/ts-snippets/app.component.snippets.pt2.ts', 'class-selected-2', 'app.component.ts (Styling each hero)')(format=".")
270+
+makeExample('toh-2/dart-snippets/app_component_snippets_pt2.dart', 'class-selected-2', 'app_component.dart (Styling each hero)')(format=".")
270271

271272
.l-sub-section
272273
:marked
@@ -283,9 +284,9 @@ code-example(format="." language="bash").
283284
:marked
284285
We select a different hero and the tell-tale color switches to that hero.
285286

286-
Here's the complete `app.component.ts` as it stands now:
287+
Here's the complete `app_component.dart` as it stands now:
287288

288-
+makeExample('toh-2/ts/app/app.component.ts', 'pt2', 'app.component.ts')
289+
+makeExample('toh-2/dart/lib/app_component.dart', 'pt2', 'app_component.dart')
289290

290291
.l-main-section
291292
:marked
@@ -296,7 +297,10 @@ code-example(format="." language="bash").
296297
* We added the ability to select a hero and show the hero’s details
297298
* We learned how to use the built-in directives `ngIf` and `ngFor` in a component’s template
298299

299-
[Run the live example for part 2](/resources/live-examples/toh-2/ts/plnkr.html)
300+
<!--
301+
TODO(kasperpeulen): Make a github repo with gh-pages for this:
302+
[Run the live example for part 2](/resources/live-examples/toh-2/dart/plnkr.html)
303+
-->
300304

301305
### The Road Ahead
302306
Our Tour of Heroes has grown, but it’s far from complete.

0 commit comments

Comments
 (0)