You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Now that we have the UI ready, let’s make the add button work.</p>
1367
1367
<h4class="exercise-start">
1368
-
<b>Exercise</b>: ???
1368
+
<b>Exercise</b>: Allow users to add groceries
1369
1369
</h4>
1370
1370
1371
1371
<p>Open <code>app/pages/list/list.html</code> and give the existing <code><TextField></code> a new <code>[(ngModel)]</code> attribute so that it looks like this:</p>
1372
1372
<pre><codeclass="lang-XML"><TextField id="grocery" [(ngModel)]="grocery" hint="Enter a grocery item" col="0"></TextField>
1373
1373
</code></pre>
1374
-
<p>Next, give image a new tap attribute binding, so that the full <code><Image></code> looks like this:</p>
<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>
<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>
<p>Next, add the following two inputs to the top of the <code>list.component.ts</code> file:</p>
1381
1381
<pre><codeclass="lang-TypeScript">import {TextField} from "ui/text-field";
@@ -1408,7 +1408,8 @@ <h4 class="exercise-start">
1408
1408
)
1409
1409
}
1410
1410
</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>
<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>""</code>.</p>
<p><imgsrc="images/chapter4/android/6.gif" alt="Adding to a list on Android">
1444
+
<imgsrc="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's look at how you can polish this page with a NativeScript module for showing activity indicators.</p>
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`andaddthefollowingpropertytothe`ListPage` class (rightbelowtheexisting`groceryList`property):
475
475
476
476
``` TypeScript
477
477
grocery: string = "";
@@ -514,7 +514,9 @@ add() {
514
514
}
515
515
```
516
516
517
-
Finally, open`app/shared/grocery/grocery-list.service.ts`andpastethefollowingfunction 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, andthencallanew`add()`methodonthe`GroceryListService`.
518
+
519
+
Tofinishthisexampleyouhavetodefinethatnew`add()`method. Todoso, open`app/shared/grocery/grocery-list.service.ts`andpastethefollowingfunction into the `GroceryService` class:
518
520
519
521
``` TypeScript
520
522
add(name:string) {
@@ -537,13 +539,27 @@ add(name: string) {
537
539
538
540
<div class="exercise-end"></div>
539
541
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, andthenusingRxJS’s`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:
541
556
542
-
Talk about the code you just wrote.
557
+

558
+

543
559
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?
545
561
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.
0 commit comments