@@ -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
3740code-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