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

Commit 01da1e1

Browse files
committed
Convert TS text to Dart
1 parent 35e48a8 commit 01da1e1

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

public/docs/dart/latest/guide/user-input.jade

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,29 @@ include ../../../../_includes/_util-fns
66
In this chapter we learn to bind to those events using the Angular
77
event binding syntax.
88

9-
[Run the live example](/resources/live-examples/user-input/ts/plnkr.html)
10-
119
:marked
1210
## Binding to user input events
1311

14-
We can use [Angular event bindings](./template-syntax.html#event-binding)
12+
We can use Angular event bindings <!-- PENDING: link to ./template-syntax.html#event-binding -->
1513
to respond to [any DOM event](https://developer.mozilla.org/en-US/docs/Web/Events).
1614

1715
The syntax is simple. We assign a template expression to the DOM event name, surrounded in parentheses.
1816
As an example, here's an event binding that implements a click handler:
19-
+makeExample('user-input/ts/app/click-me.component.ts', 'click-me-button')(format=".", language="html")
17+
+makeExample('user-input/dart/lib/click_me_component.dart', 'click-me-button')(format=".", language="html")
2018

2119
<a id="click"></a>
2220
:marked
2321
The `(click)` to the left of the equal sign identifies the button's click event as the **target of the binding**.
2422
The text within quotes on the right is the **template expression** in which we
25-
respond to the click event by calling the component's `onClickMe` method. A [template expression](./template-syntax.html#template-expressions) is a subset
26-
of JavaScript with a few added tricks.
23+
respond to the click event by calling the component's `onClickMe` method. A template expression <!-- PENDING: link to ./template-syntax.html#template-expressions --> is a subset
24+
of Dart with a few added tricks.
2725

2826
When writing a binding we must be aware of a template expression's **execution context**.
2927
The identifiers appearing within an expression belong to a specific context object.
3028
That object is usually the Angular component that controls the template ... which it definitely is
3129
in this case because that snippet of HTML belongs to the following component:
3230

33-
+makeExample('user-input/ts/app/click-me.component.ts', 'click-me-component', 'app/click-me.component.ts')(format=".")
31+
+makeExample('user-input/dart/lib/click_me_component.dart', 'click-me-component', 'web/click_me_component.dart')(format=".")
3432
:marked
3533
When the user clicks the button, Angular calls the component's `onClickMe` method.
3634

@@ -41,12 +39,22 @@ include ../../../../_includes/_util-fns
4139
what the user types back onto the screen.
4240

4341
This time we'll (1) listen to an event and (2) grab the user's input.
44-
+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-1-template', 'app/keyup.components.ts (template v.1)')(format=".")
42+
+makeExample('user-input/dart/lib/keyup_components.dart', 'key-up-component-1-template', 'web/keyup_components.dart (template v.1)')(format=".")
43+
4544
:marked
4645
Angular makes an event object available in the **`$event`** variable,
4746
which we pass to the component's `onKey()` method.
4847
The user data we want is in that variable somewhere.
49-
+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-1-class-no-type', 'app/keyup.components.ts (class v.1)')(format=".")
48+
49+
important
50+
.callout.is-important
51+
header $event vs. \$event
52+
:marked
53+
Templates in Dart files need a `\` in front of the `$`.
54+
If the template is in an HTML file, use `$event` instead of `\$event`.
55+
56+
+makeExample('user-input/dart/lib/keyup_components.dart', 'key-up-component-1-class-no-type', 'web/keyup_components.dart (class v.1)')(format=".")
57+
5058
:marked
5159
The shape of the `$event` object is determined by whatever raises the event.
5260
The `keyup` event comes from the DOM, so `$event` must be a [standard DOM event object](https://developer.mozilla.org/en-US/docs/Web/API/Event).
@@ -56,7 +64,7 @@ include ../../../../_includes/_util-fns
5664

5765
The `onKey()` component method is where we extract the user's input
5866
from the event object, adding that input to the list of user data that we're accumulating in the component's `values` property.
59-
We then use [interpolation](./template-syntax.html#interpolation)
67+
We then use interpolation <!-- PENDING: link to ./template-syntax.html#interpolation) -->
6068
to display the accumulating `values` property back on screen.
6169

6270
Enter the letters "abc", and then backspace to remove them.
@@ -70,9 +78,9 @@ figure.image-display
7078
.l-sub-section
7179
:marked
7280
We cast the `$event` as an `any` type, which means we've abandoned strong typing
73-
to simplify our code. We generally prefer the strong typing that TypeScript affords.
81+
to simplify our code. We generally prefer the strong typing that Dart affords.
7482
We can rewrite the method, casting to HTML DOM objects like this.
75-
+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-1-class', 'app/keyup.components.ts (class v.1 - strongly typed )')(format=".")
83+
+makeExample('user-input/dart/lib/keyup_components.dart', 'key-up-component-1-class', 'web/keyup_components.dart (class v.1 - strongly typed )')(format=".")
7684
:marked
7785
<br>Strong typing reveals a serious problem with passing a DOM event into the method:
7886
too much awareness of template details, too little separation of concerns.
@@ -85,13 +93,14 @@ figure.image-display
8593
## Get user input from a local template variable
8694
There's another way to get the user data without the `$event` variable.
8795

88-
Angular has a syntax feature called [**local template variables**](./template-syntax.html#local-vars).
96+
Angular has a syntax feature called **local template variables**.
97+
<!-- PENDING: link to ./template-syntax.html#local-vars) -->
8998
These variables grant us direct access to an element.
9099
We declare a local template variable by preceding an identifier with a hash/pound character (#).
91100

92101
Here's an example of using a local template variable
93102
to implement a clever keystroke loopback in an ultra-simple template.
94-
+makeExample('user-input/ts/app/loop-back.component.ts', 'loop-back-component', 'app/loop-back.component.ts')(format=".")
103+
+makeExample('user-input/dart/lib/loop_back_component.dart', 'loop-back-component', 'web/loop_back_component.dart')(format=".")
95104
:marked
96105
We've declared a template local variable named `box` on the `<input>` element.
97106
The `box` variable is a reference to the `<input>` element itself, which means we can
@@ -119,7 +128,7 @@ figure.image-display
119128
That local template variable is intriguing. It's clearly easier to get to the textbox with that
120129
variable than to go through the `$event` object. Maybe we can rewrite our previous
121130
keyup example so that it uses the variable to get the user's input. Let's give it a try.
122-
+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-2' ,'app/keyup.components.ts (v2)')(format=".")
131+
+makeExample('user-input/dart/lib/keyup_components.dart', 'key-up-component-2' ,'web/keyup_components.dart (v2)')(format=".")
123132
:marked
124133
That sure seems easier.
125134
An especially nice aspect of this approach is that our component code gets clean data values from the view.
@@ -140,7 +149,7 @@ figure.image-display
140149
Only then do we update the component's `values` property. (In this example,
141150
the update happens inside the event expression. A better practice
142151
would be to put the update code in the component.)
143-
+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-3' ,'app/keyup.components.ts (v3)')(format=".")
152+
+makeExample('user-input/dart/lib/keyup_components.dart', 'key-up-component-3' ,'web/keyup_components.dart (v3)')(format=".")
144153
:marked
145154
Here's how it works.
146155
figure.image-display
@@ -156,7 +165,7 @@ figure.image-display
156165

157166
Let's fix that by listening to the input box's blur event as well.
158167

159-
+makeExample('user-input/ts/app/keyup.components.ts', 'key-up-component-4' ,'app/keyup.components.ts (v4)')(format=".")
168+
+makeExample('user-input/dart/lib/keyup_components.dart', 'key-up-component-4' ,'web/keyup_components.dart (v4)')(format=".")
160169

161170
.l-main-section
162171
:marked
@@ -175,7 +184,7 @@ figure.image-display
175184
Below is the "Little Tour of Heroes" component.
176185
We'll call out the highlights after we bask briefly in its minimalist glory.
177186

178-
+makeExample('user-input/ts/app/little-tour.component.ts', 'little-tour', 'app/little-tour.component.ts')(format=".")
187+
+makeExample('user-input/dart/lib/little_tour_component.dart', 'little-tour', 'web/little_tour_component.dart')(format=".")
179188
:marked
180189
We've seen almost everything here before. A few things are new or bear repeating.
181190

@@ -199,7 +208,7 @@ figure.image-display
199208
The component knows nothing about HTML or the DOM, which is the way we like it.
200209

201210
### Keep template expressions simple
202-
We bound `(blur)` to *two* JavaScript statements.
211+
We bound `(blur)` to *two* Dart statements.
203212

204213
We like the first one, which calls `addHero`.
205214
We do not like the second one, which assigns an empty string to the input box value.
@@ -208,9 +217,9 @@ figure.image-display
208217
The component has no way to do that itself because it has no access to the
209218
input box (our design choice).
210219

211-
Although the example *works*, we are rightly wary of JavaScript in HTML.
220+
Although the example *works*, we are rightly wary of Dart in HTML.
212221
Template expressions are powerful. We're supposed to use them responsibly.
213-
Complex JavaScript in HTML is irresponsible.
222+
Complex Dart in HTML is irresponsible.
214223

215224
Should we reconsider our reluctance to pass the input box into the component?
216225

@@ -221,15 +230,15 @@ figure.image-display
221230

222231
Here is all the code we talked about in this chapter.
223232
+makeTabs(`
224-
user-input/ts/app/click-me.component.ts,
225-
user-input/ts/app/keyup.components.ts,
226-
user-input/ts/app/loop-back.component.ts,
227-
user-input/ts/app/little-tour.component.ts
233+
user-input/dart/lib/click_me_component.dart,
234+
user-input/dart/lib/keyup_components.dart,
235+
user-input/dart/lib/loop_back_component.dart,
236+
user-input/dart/lib/little_tour_component.dart
228237
`,'',
229-
`click-me.component.ts,
230-
keyup.components.ts,
231-
loop-back.component.ts,
232-
little-tour.component.ts`)
238+
`click_me_component.dart,
239+
keyup_components.dart,
240+
loop_back_component.dart,
241+
little_tour_component.dart`)
233242

234243
.l-main-section
235244
:marked

0 commit comments

Comments
 (0)