|
1 | 1 | include ../_util-fns
|
2 | 2 |
|
3 | 3 | :marked
|
4 |
| - We're working on the Dart version of this case study. |
5 |
| - In the meantime, please see these resources: |
| 4 | + # Once Upon a Time |
6 | 5 |
|
7 |
| - * [The Hero Editor](/docs/ts/latest/tutorial/toh-pt1.html): |
8 |
| - The TypeScript version of this chapter |
| 6 | + Every story starts somewhere. Our story starts where the [QuickStart](../quickstart.html) ends. |
9 | 7 |
|
10 |
| - * [Dart source code](https://github.com/angular/angular.io/tree/master/public/docs/_examples/toh-5/dart): |
11 |
| - A preliminary Dart version of the Tour of Heroes app, |
12 |
| - featuring the hero editor, a master/detail page, |
13 |
| - multiple components, services, and routing |
| 8 | +:marked |
| 9 | + Follow the "QuickStart" steps. They provide the prerequisites, the folder structure, |
| 10 | + and the core files for our Tour of Heroes. |
| 11 | + |
| 12 | + <!-- |
| 13 | + TODO: Recommend stagehand? |
| 14 | + --> |
| 15 | + |
| 16 | + Copy the "QuickStart" code to a new folder and rename the folder `angular2_tour_of_heroes`. |
| 17 | + We should have the following structure: |
| 18 | + |
| 19 | +.filetree |
| 20 | + .file angular2_tour_of_heroes |
| 21 | + .children |
| 22 | + .file lib |
| 23 | + .children |
| 24 | + .file app_component.dart |
| 25 | + .file web |
| 26 | + .children |
| 27 | + .file index.html |
| 28 | + .file main.dart |
| 29 | + .file pubspec.yaml |
| 30 | + |
| 31 | +// Add .file styles.css |
| 32 | +
|
| 33 | +.p |
| 34 | + |
| 35 | +.callout.is-helpful |
| 36 | + header Source code |
| 37 | + :marked |
| 38 | + The complete source code for the example app in this chapter is |
| 39 | + [in GitHub](https://github.com/angular/angular.io/tree/master/public/docs/_examples/toh-1/dart). |
| 40 | + |
| 41 | +:marked |
| 42 | + ## Keep the app compiling and running |
| 43 | + We want to start the Dart compiler, have it watch for changes, and start our server. We'll do this by typing |
| 44 | + |
| 45 | +code-example(format="" language="bash"). |
| 46 | + pub serve |
| 47 | + |
| 48 | +:marked |
| 49 | + This command runs the compiler in watch mode, starts the server, |
| 50 | + and keeps the app running while we continue to build the Tour of Heroes. |
| 51 | + |
| 52 | +.l-main-section |
| 53 | + :marked |
| 54 | + ## Show our Hero |
| 55 | + We want to display Hero data in our app. |
| 56 | + |
| 57 | + Let's add two properties to our `AppComponent`, a `title` property for the application name and a `hero` property |
| 58 | + for a hero named "Windstorm". |
| 59 | + |
| 60 | + +makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'app-component-1', 'app_component.dart (AppComponent class)')(format=".") |
| 61 | + |
| 62 | + :marked |
| 63 | + Now we update the template in the `@Component` annotation with data bindings to these new properties. |
| 64 | + |
| 65 | + +makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'show-hero') |
| 66 | + |
| 67 | + :marked |
| 68 | + The browser should refresh and display our title and hero. |
| 69 | + |
| 70 | + The double curly braces tell our app to read the `title` and `hero` properties from the component and render them. |
| 71 | + This is the "interpolation" form of one-way data binding. |
| 72 | +.l-sub-section |
| 73 | + :marked |
| 74 | + Learn more about interpolation in the [Displaying Data chapter](../guide/displaying-data.html). |
| 75 | +:marked |
| 76 | + ### Hero object |
| 77 | + |
| 78 | + At the moment, our hero is just a name. Our hero needs more properties. |
| 79 | + Let's convert the `hero` from a literal string to a class. |
| 80 | + |
| 81 | + Create a `Hero` class with `id` and `name` properties. |
| 82 | + Keep this near the top of the `app_component.dart` file for now. |
| 83 | + |
| 84 | ++makeExample('toh-1/dart/lib/app_component.dart', 'hero-class-1', 'app_component.dart (Hero class)')(format=".") |
| 85 | + |
| 86 | +:marked |
| 87 | + Now that we have a `Hero` class, let’s refactor our component’s `hero` property to be of type `Hero`. |
| 88 | + Then initialize it with an id of `1` and the name, "Windstorm". |
| 89 | + |
| 90 | ++makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'hero-property-1', 'app_component.dart (Hero property)')(format=".") |
| 91 | + |
| 92 | +:marked |
| 93 | + Because we changed the hero from a string to an object, |
| 94 | + we update the binding in the template to refer to the hero’s `name` property. |
| 95 | + |
| 96 | ++makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'show-hero-2') |
| 97 | +:marked |
| 98 | + The browser refreshes and continues to display our hero’s name. |
14 | 99 |
|
| 100 | + ### Adding more HTML |
| 101 | + Displaying a name is good, but we want to see all of our hero’s properties. |
| 102 | + We’ll add a `<div>` for our hero’s `id` property and another `<div>` for our hero’s `name`. |
| 103 | + |
| 104 | ++makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'show-hero-properties') |
| 105 | +:marked |
| 106 | + 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. |
| 107 | + |
| 108 | + ### Multi-line template strings |
| 109 | + |
| 110 | + We could make a more readable template with string concatenation |
| 111 | + but that gets ugly fast, it is harder to read, and |
| 112 | + it is easy to make a spelling error. Instead, |
| 113 | + let’s take advantage of the template strings feature |
| 114 | + in Dart to maintain our sanity. |
| 115 | + |
| 116 | + Change the quotes around the template to triple quotes and |
| 117 | + put the `<h1>`, `<h2>` and `<div>` elements on their own lines. |
| 118 | + |
| 119 | ++makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'multi-line-strings', 'app_component.dart (AppComponent\'s template)')(format=".") |
| 120 | + |
| 121 | +// omit back-tick warning |
| 122 | +
|
| 123 | +.l-main-section |
| 124 | +:marked |
| 125 | + ## Editing Our Hero |
| 126 | + |
| 127 | + We want to be able to edit the hero name in a textbox. |
| 128 | + |
| 129 | + Refactor the hero name `<label>` with `<label>` and `<input>` elements as shown below: |
| 130 | + |
| 131 | ++makeExample('toh-1/dart-snippets/app_component_snippets_pt1.dart', 'editing-Hero', 'app_component.dart (input element)')(format=".") |
| 132 | +:marked |
| 133 | + We see in the browser that the hero’s name does appear in the `<input>` textbox. |
| 134 | + But something doesn’t feel right. |
| 135 | + When we change the name, we notice that our change |
| 136 | + is not reflected in the `<h2>`. We won't get the desired behavior |
| 137 | + with a one-way binding to `<input>`. |
| 138 | + |
| 139 | + ### Two-Way Binding |
| 140 | + |
| 141 | + We intend to display the name of the hero in the `<input>`, change it, |
| 142 | + and see those changes wherever we bind to the hero’s name. |
| 143 | + In short, we want two-way data binding. |
| 144 | + |
| 145 | + Let’s update the template to use the **`ngModel`** built-in directive for two-way binding. |
| 146 | + |
| 147 | +.l-sub-section |
| 148 | + :marked |
| 149 | + Learn more about `ngModel` in the |
| 150 | + [Forms](../guide/forms.html#ngModel) and |
| 151 | + [Template Syntax](../guide/template-syntax.html#ngModel) chapters. |
| 152 | +:marked |
| 153 | + Replace the `<input>` with the following HTML |
| 154 | + |
| 155 | +code-example(language="html"). |
| 156 | + <input [(ngModel)]="hero.name" placeholder="name"> |
| 157 | + |
| 158 | +:marked |
| 159 | + The browser refreshes. We see our hero again. We can edit the hero’s name and |
| 160 | + see the changes reflected immediately in the `<h2>`. |
| 161 | + |
| 162 | +.l-main-section |
| 163 | +:marked |
| 164 | + ## The Road We’ve Travelled |
| 165 | + Let’s take stock of what we’ve built. |
| 166 | + |
| 167 | + * Our Tour of Heroes uses the double curly braces of interpolation (a form of one-way data binding) |
| 168 | + to display the application title and properties of a `Hero` object. |
| 169 | + * We wrote a multi-line template using Dart's template strings to make our template readable. |
| 170 | + * We can both display and change the hero’s name after adding a two-way data binding to the `<input>` element |
| 171 | + using the built-in `ngModel` directive. |
| 172 | + * The `ngModel` directive also propagates changes to every other binding of the `hero.name`. |
| 173 | + |
| 174 | + <!-- TODO: |
| 175 | + add [Run the live example for part 1](https://tour-of-heroes.firebaseapp.com/toh1/) |
| 176 | + --> |
| 177 | + |
| 178 | + Here's the complete `app_component.dart` as it stands now: |
| 179 | + |
| 180 | ++makeExample('toh-1/dart/lib/app_component.dart', 'pt1', 'app_component.dart') |
| 181 | + |
| 182 | +.l-main-section |
| 183 | +:marked |
| 184 | + ## The Road Ahead |
| 185 | + Our Tour of Heroes only displays one hero and we really want to display a list of heroes. |
| 186 | + We also want to allow the user to select a hero and display their details. |
| 187 | + We’ll learn more about how to retrieve lists, bind them to the |
| 188 | + template, and allow a user to select it in the |
| 189 | + [next tutorial chapter](./toh-pt2.html). |
0 commit comments