Skip to content

Commit cd69e74

Browse files
committed
Finishing up my second pass through chapter 3
1 parent a91e7c3 commit cd69e74

File tree

5 files changed

+70
-41
lines changed

5 files changed

+70
-41
lines changed

images/chapter3/android/7.gif

130 KB
Loading

images/chapter3/ios/7.gif

246 KB
Loading

images/chapter3/login_router.gif

-21.6 KB
Binary file not shown.

index.html

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,12 @@ <h4 class="exercise-start">
947947
<p><strong>TIP</strong>: If you haven’t played with Angular 2 routing before, you can refer to <a href="https://angular.io/docs/ts/latest/tutorial/toh-pt5.html">Angular’s tutorial on the topic</a> for some background.</p>
948948
</blockquote>
949949
<p>The other new concept in this example is <code>&lt;page-router-outlet&gt;</code>, which is your app’s first directive. You can check out Angular’s docs for <a href="https://angular.io/docs/ts/latest/api/core/Directive-decorator.html">details on what directives do</a>, but the simplest way to think of them is as something that can affect the markup you put in your <code>template</code>—in this case <code>&lt;page-router-outlet&gt;</code>.</p>
950-
<p>Angular 2 provides a <code>&lt;router-outlet&gt;</code> directive for web apps, and NativeScript extends that directive with its own <code>&lt;page-router-outlet&gt;</code> directive that handles the unique environment of iOS and Android apps. To see how it works lets add another page.</p>
950+
<p>Angular 2 provides a <code>&lt;router-outlet&gt;</code> directive for web apps, and NativeScript extends that directive with its own <code>&lt;page-router-outlet&gt;</code> directive that handles the unique environment of iOS and Android apps. To see how it works let’s add another page.</p>
951951
<h4 class="exercise-start">
952952
<b>Exercise</b>: Create the list page
953953
</h4>
954954

955-
<p>Open pages/list/list.component.ts and paste in this:</p>
955+
<p>Open <code>pages/list/list.component.ts</code> and paste in the following code, which you’ll use as the start of a simple list page:</p>
956956
<pre><code class="lang-TypeScript">import {Component} from &quot;angular2/core&quot;;
957957

958958
@Component({
@@ -962,16 +962,20 @@ <h4 class="exercise-start">
962962
})
963963
export class ListPage {}
964964
</code></pre>
965-
<p>Next, open pages/list/list.html and paste in this:</p>
965+
<p>For now, we’ll keep the list page simple so you can see how the routing works. But so that there’s something to see, open <code>pages/list/list.html</code> and paste the following label:</p>
966966
<pre><code class="lang-XML">&lt;Label text=&quot;Hello world&quot;&gt;&lt;/Label&gt;
967967
</code></pre>
968-
<p>Now, go back to app/app.component.ts and paste in this at the top of the file:</p>
968+
<p>After that, go back to <code>app/app.component.ts</code> and paste the following import in at the top of the file:</p>
969969
<pre><code class="lang-TypeScript">import {ListPage} from &quot;./pages/list/list.component&quot;;
970970
</code></pre>
971-
<p>And this in the <code>@RouteConfig</code>:</p>
972-
<pre><code class="lang-TypeScript">{ path: &quot;/List&quot;, component: ListPage, as: &quot;List&quot; }
971+
<p>Next, replace the existing <code>@RouteConfig</code> with the following code so that it includes this new list page:</p>
972+
<pre><code class="lang-TypeScript">@RouteConfig([
973+
{ path: &quot;/Login&quot;, component: LoginPage, as: &quot;Login&quot;, useAsDefault: true },
974+
{ path: &quot;/List&quot;, component: ListPage, as: &quot;List&quot; }
975+
])
973976
</code></pre>
974-
<p>Now go to app/shared/user/user.service.ts and add this function:</p>
977+
<p>Angular 2 now knows about the list page, but we still need to navigate the user to that page at the appropriate time. Our next step is to allow users to log into their accounts, and to take navigate them to the new list page after they successfully authenticate.</p>
978+
<p>To do that, start by opening <code>app/shared/user/user.service.ts</code> and the <code>login()</code> function below to the existing <code>UserService</code> class:</p>
975979
<pre><code class="lang-TypeScript">login(user: User) {
976980
var headers = new Headers();
977981
headers.append(&quot;Content-Type&quot;, &quot;application/json&quot;);
@@ -992,25 +996,36 @@ <h4 class="exercise-start">
992996
.catch(this.handleErrors);
993997
}
994998
</code></pre>
995-
<p>Finally, go to <code>app/login/login.component.ts</code> and change the login function to do this:</p>
996-
<pre><code class="lang-TypeScript">this._userService.login(this.user)
997-
.subscribe(
998-
() =&gt; this._router.navigate([&quot;List&quot;]),
999-
(error) =&gt; alert(&quot;Unfortunately we could not find your account.&quot;)
1000-
);
1001-
</code></pre>
1002-
<p>To make this work, in the same file, add this to the top:</p>
999+
<p>This code hits one of our existing backend endpoints, and stores off a authentication token that we’ll use later in this guide.</p>
1000+
<p>To use this <code>login()</code> function, return to <code>app/login/login.component.ts</code>, and add the following import to the top of the file:</p>
10031001
<pre><code class="lang-TypeScript">import {Router} from &quot;angular2/router&quot;;
10041002
</code></pre>
1005-
<p>And this to the constructor:</p>
1003+
<p>Next, replace the current <code>constructor()</code> declaration with the code below, which adds Angular 2’s <code>Router</code> service:</p>
10061004
<pre><code class="lang-TypeScript">constructor(private _router: Router, private _userService: UserService) {
10071005
</code></pre>
1006+
<p>Finally, replace the <code>LoginPage</code>’s existing <code>login()</code> function with the code below:</p>
1007+
<pre><code class="lang-TypeScript">login() {
1008+
this._userService.login(this.user)
1009+
.subscribe(
1010+
() =&gt; this._router.navigate([&quot;List&quot;]),
1011+
(error) =&gt; alert(&quot;Unfortunately we could not find your account.&quot;)
1012+
);
1013+
}
1014+
</code></pre>
1015+
<blockquote>
1016+
<p><strong>NOTE</strong>:</p>
1017+
<ul>
1018+
<li>You don’t have to add <code>Router</code> to your <code>LoginPage</code> component’s <code>providers</code> array because it’s already included in the parent <code>AppComponent</code> component’s <code>providers</code> list.</li>
1019+
<li>Refer to Angular’s documentation for a <a href="https://angular.io/docs/ts/latest/api/router/Router-class.html">full list of the API available on the <code>Router</code> service</a>.</li>
1020+
</ul>
1021+
</blockquote>
10081022
<div class="exercise-end"></div>
10091023

1010-
<p>You can login now! And navigate! </p>
1011-
<p><img alt="Login with basic information" src="images/chapter3/login_router.gif"></p>
1012-
<p>Talk about how in NativeScript you get native behavior automatically—aka a back button on iOS and a hardware back button on Android.</p>
1013-
<p>Then transition to modules.</p>
1024+
<p>After this change you can now navigate between the login and list pages in your app:</p>
1025+
<p><img src="images/chapter3/android/7.gif" alt="Navigating on Android">
1026+
<img src="images/chapter3/ios/7.gif" alt="Navigating on iOS"></p>
1027+
<p>The power of NativeScript is you have the ability to use the same Angular conventions that you’d use in a web app—<code>@RouteConfig</code>, <code>Router</code>, and so forth—yet get an app that fits right in on iOS and Android. Notice how on Android the hardware back button works as expected, and how your iOS app uses built-in iOS animations and conventions such as the back button.</p>
1028+
<p>And we’re just getting started. NativeScript provides a number of other ways to tie into native device functionality out of the box through NativeScript modules. Let’s look at how they work.</p>
10141029

10151030
</div>
10161031
<div class="chapter">

src/chapters/chapter3.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,13 @@ The main new concept here is the `@RouteConfig` decorator, which you use to prov
531531
532532
The other new concept in this example is `<page-router-outlet>`, which is your app’s first directive. You can check out Angular’s docs for [details on what directives do](https://angular.io/docs/ts/latest/api/core/Directive-decorator.html), but the simplest way to think of them is as something that can affect the markup you put in your `template`—in this case `<page-router-outlet>`.
533533

534-
Angular 2 provides a `<router-outlet>` directive for web apps, and NativeScript extends that directive with its own `<page-router-outlet>` directive that handles the unique environment of iOS and Android apps. To see how it works lets add another page.
534+
Angular 2 provides a `<router-outlet>` directive for web apps, and NativeScript extends that directive with its own `<page-router-outlet>` directive that handles the unique environment of iOS and Android apps. To see how it works let’s add another page.
535535

536536
<h4 class="exercise-start">
537537
<b>Exercise</b>: Create the list page
538538
</h4>
539539

540-
Open pages/list/list.component.ts and paste in this:
540+
Open `pages/list/list.component.ts` and paste in the following code, which you’ll use as the start of a simple list page:
541541

542542
``` TypeScript
543543
import {Component} from "angular2/core";
@@ -550,25 +550,30 @@ import {Component} from "angular2/core";
550550
export class ListPage {}
551551
```
552552

553-
Next, open pages/list/list.html and paste in this:
553+
For now, we’ll keep the list page simple so you can see how the routing works. But so that there’s something to see, open `pages/list/list.html` and paste the following label:
554554

555555
``` XML
556556
<Label text="Hello world"></Label>
557557
```
558558

559-
Now, go back to app/app.component.ts and paste in this at the top of the file:
559+
After that, go back to `app/app.component.ts` and paste the following import in at the top of the file:
560560

561561
``` TypeScript
562562
import {ListPage} from "./pages/list/list.component";
563563
```
564564

565-
And this in the `@RouteConfig`:
565+
Next, replace the existing `@RouteConfig` with the following code so that it includes this new list page:
566566

567567
``` TypeScript
568-
{ path: "/List", component: ListPage, as: "List" }
568+
@RouteConfig([
569+
{ path: "/Login", component: LoginPage, as: "Login", useAsDefault: true },
570+
{ path: "/List", component: ListPage, as: "List" }
571+
])
569572
```
570573

571-
Now go to app/shared/user/user.service.ts and add this function:
574+
Angular 2 now knows about the list page, but we still need to navigate the user to that page at the appropriate time. Our next step is to allow users to log into their accounts, and to take navigate them to the new list page after they successfully authenticate.
575+
576+
To do that, start by opening `app/shared/user/user.service.ts` and the `login()` function below to the existing `UserService` class:
572577

573578
``` TypeScript
574579
login(user: User) {
@@ -592,34 +597,43 @@ login(user: User) {
592597
}
593598
```
594599

595-
Finally, go to `app/login/login.component.ts` and change the login function to do this:
600+
This code hits one of our existing backend endpoints, and stores off a authentication token that we’ll use later in this guide.
601+
602+
To use this `login()` function, return to `app/login/login.component.ts`, and add the following import to the top of the file:
596603

597604
``` TypeScript
598-
this._userService.login(this.user)
599-
.subscribe(
600-
() => this._router.navigate(["List"]),
601-
(error) => alert("Unfortunately we could not find your account.")
602-
);
605+
import {Router} from "angular2/router";
603606
```
604607

605-
To make this work, in the same file, add this to the top:
608+
Next, replace the current `constructor()` declaration with the code below, which adds Angular 2’s `Router` service:
606609

607610
``` TypeScript
608-
import {Router} from "angular2/router";
611+
constructor(private _router: Router, private _userService: UserService) {
609612
```
610613
611-
And this to the constructor:
614+
Finally, replace the `LoginPage`’s existing `login()` function with the code below:
612615
613616
``` TypeScript
614-
constructor(private _router: Router, private _userService: UserService) {
617+
login() {
618+
this._userService.login(this.user)
619+
.subscribe(
620+
() => this._router.navigate(["List"]),
621+
(error) => alert("Unfortunately we could not find your account.")
622+
);
623+
}
615624
```
616625
626+
> **NOTE**:
627+
> * You don’t have to add `Router` to your `LoginPage` component’s `providers` array because it’s already included in the parent `AppComponent` component’s `providers` list.
628+
> * Refer to Angular’s documentation for a [full list of the API available on the `Router` service](https://angular.io/docs/ts/latest/api/router/Router-class.html).
629+
617630
<div class="exercise-end"></div>
618631
619-
You can login now! And navigate!
632+
After this change you can now navigate between the login and list pages in your app:
620633
621-
<img alt="Login with basic information" src="images/chapter3/login_router.gif">
634+
![Navigating on Android](images/chapter3/android/7.gif)
635+
![Navigating on iOS](images/chapter3/ios/7.gif)
622636
623-
Talk about how in NativeScript you get native behavior automatically—aka a back button on iOS and a hardware back button on Android.
637+
The power of NativeScript is you have the ability to use the same Angular conventions that you’d use in a web app—`@RouteConfig`, `Router`, and so forth—yet get an app that fits right in on iOS and Android. Notice how on Android the hardware back button works as expected, and how your iOS app uses built-in iOS animations and conventions such as the back button.
624638
625-
Then transition to modules.
639+
And we’re just getting started. NativeScript provides a number of other ways to tie into native device functionality out of the box through NativeScript modules. Let’s look at how they work.

0 commit comments

Comments
 (0)