Skip to content

Commit 935dab5

Browse files
committed
Finishing my second pass through 4.3
1 parent c29ef04 commit 935dab5

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

images/chapter4/android/6.gif

124 KB
Loading

images/chapter4/ios/6.gif

184 KB
Loading

index.html

+22-10
Original file line numberDiff line numberDiff line change
@@ -1365,17 +1365,17 @@ <h4 class="exercise-start">
13651365
</blockquote>
13661366
<p>Now that we have the UI ready, let’s make the add button work.</p>
13671367
<h4 class="exercise-start">
1368-
<b>Exercise</b>: ???
1368+
<b>Exercise</b>: Allow users to add groceries
13691369
</h4>
13701370

13711371
<p>Open <code>app/pages/list/list.html</code> and give the existing <code>&lt;TextField&gt;</code> a new <code>[(ngModel)]</code> attribute so that it looks like this:</p>
13721372
<pre><code class="lang-XML">&lt;TextField id=&quot;grocery&quot; [(ngModel)]=&quot;grocery&quot; hint=&quot;Enter a grocery item&quot; col=&quot;0&quot;&gt;&lt;/TextField&gt;
13731373
</code></pre>
1374-
<p>Next, give image a new tap attribute binding, so that the full <code>&lt;Image&gt;</code> looks like this:</p>
1375-
<pre><code class="lang-XML">&lt;Image src=&quot;res://add&quot; id=&quot;add-image&quot; (tap)=&quot;add()&quot; col=&quot;1&quot;&gt;&lt;/Image&gt;
1374+
<p>Next, give the same file’s image a new <code>tap</code> attribute binding, so that the full <code>&lt;Image&gt;</code> looks like this:</p>
1375+
<pre><code class="lang-XML">&lt;Image src=&quot;res://add&quot; (tap)=&quot;add()&quot; col=&quot;1&quot;&gt;&lt;/Image&gt;
13761376
</code></pre>
1377-
<p>Next, open <code>app/pages/list/list.component.ts</code> and add the following property to the <code>ListPage</code> class (right below <code>groceryList</code>):</p>
1378-
<pre><code class="lang-TypeScript">grocery: string;
1377+
<p>With these attributes in place, your next steps are to define the <code>grocery</code> property and the <code>add()</code> method. To do that, open <code>app/pages/list/list.component.ts</code> and add the following property to the <code>ListPage</code> class (right below the existing <code>groceryList</code> property):</p>
1378+
<pre><code class="lang-TypeScript">grocery: string = &quot;&quot;;
13791379
</code></pre>
13801380
<p>Next, add the following two inputs to the top of the <code>list.component.ts</code> file:</p>
13811381
<pre><code class="lang-TypeScript">import {TextField} from &quot;ui/text-field&quot;;
@@ -1408,7 +1408,8 @@ <h4 class="exercise-start">
14081408
)
14091409
}
14101410
</code></pre>
1411-
<p>Finally, open <code>app/shared/grocery/grocery-list.service.ts</code> and paste the following function into the <code>GroceryService</code> class:</p>
1411+
<p>In this function you first ensure the user didn’t submit without typing a grocery. If the user did type something, you dismiss the device’s keyboard with the TextField element’s <code>dismissSoftInput()</code> method, and then call a new <code>add()</code> method on the <code>GroceryListService</code>.</p>
1412+
<p>To finish this example you have to define that new <code>add()</code> method. To do so, open <code>app/shared/grocery/grocery-list.service.ts</code> and paste the following function into the <code>GroceryService</code> class:</p>
14121413
<pre><code class="lang-TypeScript">add(name: string) {
14131414
var headers = new Headers();
14141415
headers.append(&quot;Authorization&quot;, &quot;Bearer &quot; + Config.token);
@@ -1428,10 +1429,21 @@ <h4 class="exercise-start">
14281429
</code></pre>
14291430
<div class="exercise-end"></div>
14301431

1431-
<p>Remind people that the final code for the tutorial is up on GitHub.</p>
1432-
<p>Talk about the code you just wrote.</p>
1433-
<p>Show a gif of the page in action.</p>
1434-
<p>Let’s make the page look a little nicer.</p>
1432+
<p>The <code>add()</code> code should look familiar as you’re again using the <code>Http</code> service’s <code>post()</code> method to make an HTTP call to our backend, and then using RxJS’s <code>map()</code> function to convert the returned data into a <code>Grocery</code> object. You consume that object in the <code>ListPage</code> component’s <code>add()</code> method, which adds the grocery to the page’s list by calling <code>this.groceryList.unshift()</code>, and then empties that page’s text field by setting <code>this.grocery</code> equal to <code>&quot;&quot;</code>.</p>
1433+
<pre><code class="lang-TypeScript">this._groceryListService.add(this.grocery)
1434+
.subscribe(
1435+
groceryObject =&gt; {
1436+
this.groceryList.unshift(groceryObject);
1437+
this.grocery = &quot;&quot;;
1438+
},
1439+
() =&gt; { ... }
1440+
);
1441+
</code></pre>
1442+
<p>The end result looks like this:</p>
1443+
<p><img src="images/chapter4/android/6.gif" alt="Adding to a list on Android">
1444+
<img src="images/chapter4/ios/6.gif" alt="Adding to a list on iOS"></p>
1445+
<p>At this point you can add a grocery item and it will appear immediately in your list—and, all of this is completely driven by a backend service. Pretty cool, huh?</p>
1446+
<p>Let&#39;s look at how you can polish this page with a NativeScript module for showing activity indicators.</p>
14351447
<h3 id="activityindicator">ActivityIndicator</h3>
14361448
<p>Introduce the activity indicator.</p>
14371449
<h4 class="exercise-start">

src/chapters/chapter4.md

+25-9
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ Here the `columns` attribute divides the top of the screen into two columns. The
456456
Now that we have the UI ready, let’s make the add button work.
457457
458458
<h4 class="exercise-start">
459-
<b>Exercise</b>: ???
459+
<b>Exercise</b>: Allow users to add groceries
460460
</h4>
461461
462462
Open `app/pages/list/list.html` and give the existing `<TextField>` a new `[(ngModel)]` attribute so that it looks like this:
@@ -465,13 +465,13 @@ Open `app/pages/list/list.html` and give the existing `<TextField>` a new `[(ngM
465465
<TextField id="grocery" [(ngModel)]="grocery" hint="Enter a grocery item" col="0"></TextField>
466466
```
467467
468-
Next, give image a new tap attribute binding, so that the full `<Image>` looks like this:
468+
Next, give the same file’s image a new `tap` attribute binding, so that the full `<Image>` looks like this:
469469
470470
``` XML
471-
<Image src="res://add" id="add-image" (tap)="add()" col="1"></Image>
471+
<Image src="res://add" (tap)="add()" col="1"></Image>
472472
```
473473
474-
Next, open `app/pages/list/list.component.ts` and add the following property to the `ListPage` class (right below `groceryList`):
474+
With these attributes in place, your next steps are to define the `grocery` property and the `add()` method. To do that, open `app/pages/list/list.component.ts` and add the following property to the `ListPage` class (right below the existing `groceryList` property):
475475

476476
``` TypeScript
477477
grocery: string = "";
@@ -514,7 +514,9 @@ add() {
514514
}
515515
```
516516
517-
Finally, open `app/shared/grocery/grocery-list.service.ts` and paste the following function into the `GroceryService` class:
517+
In this function you first ensure the user didn’t submit without typing a grocery. If the user did type something, you dismiss the device’s keyboard with the TextField element’s `dismissSoftInput()` method, and then call a new `add()` method on the `GroceryListService`.
518+
519+
To finish this example you have to define that new `add()` method. To do so, open `app/shared/grocery/grocery-list.service.ts` and paste the following function into the `GroceryService` class:
518520

519521
``` TypeScript
520522
add(name: string) {
@@ -537,13 +539,27 @@ add(name: string) {
537539
538540
<div class="exercise-end"></div>
539541
540-
Remind people that the final code for the tutorial is up on GitHub.
542+
The `add()` code should look familiar as you’re again using the `Http` service’s `post()` method to make an HTTP call to our backend, and then using RxJSs `map()` function to convert the returned data into a `Grocery` object. You consume that object in the `ListPage` component’s `add()` method, which adds the grocery to the page’s list by calling `this.groceryList.unshift()`, and then empties that page’s text field by setting `this.grocery` equal to `""`.
543+
544+
``` TypeScript
545+
this._groceryListService.add(this.grocery)
546+
.subscribe(
547+
groceryObject => {
548+
this.groceryList.unshift(groceryObject);
549+
this.grocery = "";
550+
},
551+
() => { ... }
552+
);
553+
```
554+
555+
The end result looks like this:
541556
542-
Talk about the code you just wrote.
557+
![Adding to a list on Android](images/chapter4/android/6.gif)
558+
![Adding to a list on iOS](images/chapter4/ios/6.gif)
543559
544-
Show a gif of the page in action.
560+
At this point you can add a grocery item and it will appear immediately in your list—and, all of this is completely driven by a backend service. Pretty cool, huh?
545561
546-
Let’s make the page look a little nicer.
562+
Let's look at how you can polish this page with a NativeScript module for showing activity indicators.
547563
548564
### ActivityIndicator
549565

0 commit comments

Comments
 (0)