Skip to content

Commit cf762b7

Browse files
committed
Finishing edits of section 3.2.
1 parent 834193f commit cf762b7

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

index.html

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ <h4 class="exercise-start">
653653
<h3 id="structuring-your-app">Structuring your app</h3>
654654
<p>There are many reasons to segment any application into modular units, and you can <a href="https://en.wikipedia.org/wiki/Modular_programming">read about the various benefits on Wikipedia</a>. Modularizing NativeScript apps, in addition, has one unique benefit: the ability to share the code you write between Angular-2-built web apps, and Angular-2-built native apps.</p>
655655
<p>Even if you have no plans to create an Angular 2 web app, separating out your code is still advantageous for a number of other reasons—testability, ease of maintenance, and so forth—but if you <em>do</em> have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse in your native and web apps can be an invaluable time saver.</p>
656-
<p>To see how this works in action, let’s edit some files in the <code>/shared</code> folder and set them up to be imported.</p>
656+
<p>To see how this works in action, let’s edit some files in the <code>shared</code> folder and set them up to be imported.</p>
657657
<h4 class="exercise-start">
658658
<b>Exercise</b>: Add a model object
659659
</h4>
@@ -665,12 +665,11 @@ <h4 class="exercise-start">
665665
}
666666
</code></pre>
667667
<p>This code defines a simple <a href="http://www.typescriptlang.org/Handbook#classes">TypeScript class</a> that does nothing more than define two properties—<code>email</code> and <code>password</code>. Note the use of <a href="http://www.typescriptlang.org/Handbook#modules-going-external">TypeScript’s <code>export</code> keyword</a>, as we’ll see why that’s important momentarily.</p>
668-
<p>Next, open <code>app/app.component.ts</code>, and first add the following <code>import</code> to the top of the file:</p>
668+
<p>Next, open <code>app/app.component.ts</code>, and add the following <code>import</code> to the top of the file:</p>
669669
<pre><code class="lang-JavaScript">import {User} from &quot;./shared/user/user&quot;;
670670
</code></pre>
671671
<p>Here you import the <code>User</code> class that you just defined. Note the parallel between the <code>export</code> command used in the previous example and the <code>import</code> command used here. The reason the <code>User</code> class is available to import is because it was explicitly exported. You’ll see other examples of <code>import</code> and <code>export</code> as you go through this guide.</p>
672672
<p>Next, replace the existing <code>AppComponent</code> definition with the one below, which uses the <code>User</code> class you just imported.</p>
673-
<!-- Todo, explain the isLoggingIn piece - from the exercise above I think. The button XML needs to be edited -->
674673
<pre><code class="lang-JavaScript">export class AppComponent {
675674
user: User;
676675
isLoggingIn = true;
@@ -687,14 +686,28 @@ <h4 class="exercise-start">
687686
}
688687
</code></pre>
689688
<p>Instead of storing data on the <code>AppComponent</code> directly, you’re now using the <code>User</code> model object, which is reusable outside of this page and even outside of this application. You instantiate an instance of the <code>User</code> class in a new <code>constructor</code> function, which Angular 2 invokes when it bootstraps your application.</p>
690-
<p>Your final step is to use this new model object in your template. To do that, replace the two existing <code>&lt;TextField&gt;</code>s with the code shown below, which updates the <code>[(ngModel)]</code> bindings to point at the new <code>User</code> object:</p>
689+
<p>Your next step is to use this new model object in your template. To do that, replace the two existing <code>&lt;TextField&gt;</code>s with the code shown below, which updates the <code>[(ngModel)]</code> bindings to point at the new <code>User</code> object:</p>
691690
<pre><code class="lang-XML">&lt;TextField hint=&quot;Email Address&quot; id=&quot;email&quot; keyboardType=&quot;email&quot; [(ngModel)]=&quot;user.email&quot;
692691
autocorrect=&quot;false&quot; autocapitalizationType=&quot;none&quot;&gt;&lt;/TextField&gt;
693692
&lt;TextField hint=&quot;Password&quot; id=&quot;password&quot; secure=&quot;true&quot; [(ngModel)]=&quot;user.password&quot;&gt;&lt;/TextField&gt;
694693
</code></pre>
695-
<p>TODO: Better explanation. Move the full template into the login.html file and use <code>templateUrl</code>. </p>
696-
<p>If you got lost during this section, here’s a copy-and-paste friendly version of the full <code>app.component.ts</code> you should have at this point:</p>
697-
<pre><code class="lang-JavaScript">import {Component} from &quot;angular2/core&quot;;
694+
<p>As one final change, because this template is getting to be a bit complex, let’s move it into a separate file. Open <code>app/pages/login/login.html</code> and paste in the following code:</p>
695+
<pre><code class="lang-XML">&lt;StackLayout&gt;
696+
&lt;Image src=&quot;res://logo_login&quot; stretch=&quot;none&quot; horizontalAlignment=&quot;center&quot;&gt;&lt;/Image&gt;
697+
698+
&lt;TextField hint=&quot;Email Address&quot; id=&quot;email&quot; keyboardType=&quot;email&quot; [(ngModel)]=&quot;user.email&quot;
699+
autocorrect=&quot;false&quot; autocapitalizationType=&quot;none&quot;&gt;&lt;/TextField&gt;
700+
&lt;TextField hint=&quot;Password&quot; id=&quot;password&quot; secure=&quot;true&quot; [(ngModel)]=&quot;user.password&quot;&gt;&lt;/TextField&gt;
701+
702+
&lt;Button [text]=&quot;isLoggingIn ? &#39;Sign in&#39; : &#39;Sign up&#39;&quot; id=&quot;submit-button&quot; (tap)=&quot;submit()&quot;&gt;&lt;/Button&gt;
703+
&lt;Button [text]=&quot;isLoggingIn ? &#39;Sign up&#39; : &#39;Back to login&#39;&quot; (tap)=&quot;toggleDisplay()&quot;&gt;&lt;/Button&gt;
704+
&lt;/StackLayout&gt;
705+
</code></pre>
706+
<p>Finally, in <code>app/app.component.ts</code>, replace the existing <code>template</code> property with the new <code>templateUrl</code> property shown below:</p>
707+
<pre><code class="lang-TypeScript">templateUrl: &quot;pages/login/login.html&quot;
708+
</code></pre>
709+
<p>In case you got lost during this section, here’s a copy-and-paste friendly of the <code>app/app.component.ts</code> file you should have at this point:</p>
710+
<pre><code class="lang-TypeScript">import {Component} from &quot;angular2/core&quot;;
698711
import {User} from &quot;./shared/user/user&quot;;
699712

700713
@Component({
@@ -719,6 +732,9 @@ <h4 class="exercise-start">
719732
</code></pre>
720733
<div class="exercise-end"></div>
721734

735+
<blockquote>
736+
<p><strong>TIP</strong>: With Angular 2 components you have the ability to specify templates and CSS styling in two places—directly within the component, or in external files. For simple components feel free to choose either approach based on your personal preference, but once your templates/styles get to ~10 lines of code, consider using external files exclusively, as mixing non-trivial XML, CSS, and JavaScript code makes your component code less readable.</p>
737+
</blockquote>
722738
<p>With this setup you now have a <code>User</code> class that you can share across pages in your app and even across applications. But a model object that’s four simple lines of code isn’t all that exciting. Where this approach really pays off is when you’re able to share your business logic, and the code that hits your backend systems. In Angular 2 those classes are known as services. Let’s look at them next.</p>
723739
<h3 id="services">Services</h3>
724740
<p>A login screen isn’t all that useful if it doesn’t actually log users into anything. Therefore, our next task is to take the user’s email address and password, and send them to a backend endpoint to retrieve an authentication token we’ll use later in this guide. We’ll build this functionality as an Angular 2 service.</p>

src/chapters/chapter3.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ There are many reasons to segment any application into modular units, and you ca
151151

152152
Even if you have no plans to create an Angular 2 web app, separating out your code is still advantageous for a number of other reasons—testability, ease of maintenance, and so forth—but if you _do_ have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse in your native and web apps can be an invaluable time saver.
153153

154-
To see how this works in action, let’s edit some files in the `/shared` folder and set them up to be imported.
154+
To see how this works in action, let’s edit some files in the `shared` folder and set them up to be imported.
155155

156156
<h4 class="exercise-start">
157157
<b>Exercise</b>: Add a model object
@@ -168,7 +168,7 @@ export class User {
168168

169169
This code defines a simple [TypeScript class](http://www.typescriptlang.org/Handbook#classes) that does nothing more than define two properties—`email` and `password`. Note the use of [TypeScript’s `export` keyword](http://www.typescriptlang.org/Handbook#modules-going-external), as we’ll see why that’s important momentarily.
170170

171-
Next, open `app/app.component.ts`, and first add the following `import` to the top of the file:
171+
Next, open `app/app.component.ts`, and add the following `import` to the top of the file:
172172

173173
``` JavaScript
174174
import {User} from "./shared/user/user";
@@ -178,7 +178,6 @@ Here you import the `User` class that you just defined. Note the parallel betwee
178178

179179
Next, replace the existing `AppComponent` definition with the one below, which uses the `User` class you just imported.
180180

181-
<!-- Todo, explain the isLoggingIn piece - from the exercise above I think. The button XML needs to be edited -->
182181
``` JavaScript
183182
export class AppComponent {
184183
user: User;
@@ -198,19 +197,38 @@ export class AppComponent {
198197

199198
Instead of storing data on the `AppComponent` directly, you’re now using the `User` model object, which is reusable outside of this page and even outside of this application. You instantiate an instance of the `User` class in a new `constructor` function, which Angular 2 invokes when it bootstraps your application.
200199

201-
Your final step is to use this new model object in your template. To do that, replace the two existing `<TextField>`s with the code shown below, which updates the `[(ngModel)]` bindings to point at the new `User` object:
200+
Your next step is to use this new model object in your template. To do that, replace the two existing `<TextField>`s with the code shown below, which updates the `[(ngModel)]` bindings to point at the new `User` object:
202201

203202
``` XML
204203
<TextField hint="Email Address" id="email" keyboardType="email" [(ngModel)]="user.email"
205204
autocorrect="false" autocapitalizationType="none"></TextField>
206205
<TextField hint="Password" id="password" secure="true" [(ngModel)]="user.password"></TextField>
207206
```
208207

209-
TODO: Better explanation. Move the full template into the login.html file and use `templateUrl`.
208+
As one final change, because this template is getting to be a bit complex, let’s move it into a separate file. Open `app/pages/login/login.html` and paste in the following code:
210209

211-
If you got lost during this section, here’s a copy-and-paste friendly version of the full `app.component.ts` you should have at this point:
210+
``` XML
211+
<StackLayout>
212+
<Image src="res://logo_login" stretch="none" horizontalAlignment="center"></Image>
212213

213-
``` JavaScript
214+
<TextField hint="Email Address" id="email" keyboardType="email" [(ngModel)]="user.email"
215+
autocorrect="false" autocapitalizationType="none"></TextField>
216+
<TextField hint="Password" id="password" secure="true" [(ngModel)]="user.password"></TextField>
217+
218+
<Button [text]="isLoggingIn ? 'Sign in' : 'Sign up'" id="submit-button" (tap)="submit()"></Button>
219+
<Button [text]="isLoggingIn ? 'Sign up' : 'Back to login'" (tap)="toggleDisplay()"></Button>
220+
</StackLayout>
221+
```
222+
223+
Finally, in `app/app.component.ts`, replace the existing `template` property with the new `templateUrl` property shown below:
224+
225+
``` TypeScript
226+
templateUrl: "pages/login/login.html"
227+
```
228+
229+
In case you got lost during this section, here’s a copy-and-paste friendly of the `app/app.component.ts` file you should have at this point:
230+
231+
``` TypeScript
214232
import {Component} from "angular2/core";
215233
import {User} from "./shared/user/user";
216234

@@ -233,11 +251,12 @@ export class AppComponent {
233251
this.isLoggingIn = !this.isLoggingIn;
234252
}
235253
}
236-
237254
```
238255

239256
<div class="exercise-end"></div>
240257

258+
> **TIP**: With Angular 2 components you have the ability to specify templates and CSS styling in two places—directly within the component, or in external files. For simple components feel free to choose either approach based on your personal preference, but once your templates/styles get to ~10 lines of code, consider using external files exclusively, as mixing non-trivial XML, CSS, and JavaScript code makes your component code less readable.
259+
241260
With this setup you now have a `User` class that you can share across pages in your app and even across applications. But a model object that’s four simple lines of code isn’t all that exciting. Where this approach really pays off is when you’re able to share your business logic, and the code that hits your backend systems. In Angular 2 those classes are known as services. Let’s look at them next.
242261

243262
### Services

0 commit comments

Comments
 (0)