Skip to content

Commit 451faf7

Browse files
committed
Filling out the files and structure
1 parent 55e27e1 commit 451faf7

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

index.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,69 @@ <h3 id="structuring-your-app">Structuring your app</h3>
559559
<p>There are many reasons to segment your application into modular units, and you can <a href="https://en.wikipedia.org/wiki/Modular_programming">read about the various benefits on Wikipedia</a>. However, keeping NativeScript apps modular has one unique benefit: the ability to share the code you write between Angular-2-built web apps, and Angular-2-build native apps.</p>
560560
<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 for your native and web apps can be an invaluable time saver.</p>
561561
<p>To see how this works in action let’s create a few files.</p>
562+
<h4 class="exercise-start">
563+
<b>Exercise</b>: Add a model object
564+
</h4>
565+
566+
<p>The first thing we’ll do is add a model object to store user data. Open <code>app/user/user.service.ts</code> and paste in the following code:</p>
567+
<pre><code class="lang-JavaScript">export class User {
568+
email: string;
569+
password: string;
570+
}
571+
</code></pre>
572+
<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>
573+
<p>Next, open <code>app/app.component.ts</code>, and first add the following <code>import</code> to the top of the file:</p>
574+
<pre><code class="lang-JavaScript">import {User} from &quot;./shared/user/user&quot;;
575+
</code></pre>
576+
<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 in the previous example. 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>
577+
<p>Next, replace the existing <code>AppComponent</code> definition with the one below, which uses the <code>User</code> class you just imported.</p>
578+
<pre><code class="lang-JavaScript">export class AppComponent {
579+
user: User;
580+
constructor() {
581+
this.user = new User();
582+
}
583+
signIn() {
584+
alert(&quot;You’re using: &quot; + this.user.email);
585+
}
586+
}
587+
</code></pre>
588+
<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 also provide a TypeScript constructor, which is a function invoked when an instance of the <code>AppComponent</code> class is instantiated. In this app, Angular 2 instantiates an <code>AppComponent</code> for you with the call to <code>nativeScriptBootstrap(AppComponent)</code> done in <code>main.ts</code>. </p>
589+
<p>Your final step is to use this new model object in your template. To do that, replace the existing two <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>
590+
<pre><code class="lang-XML">&lt;TextField hint=&quot;Email Address&quot; keyboardType=&quot;email&quot; [(ngModel)]=&quot;user.email&quot;
591+
autocorrect=&quot;false&quot; autocapitalizationType=&quot;none&quot;&gt;&lt;/TextField&gt;
592+
&lt;TextField hint=&quot;Password&quot; secure=&quot;true&quot; [(ngModel)]=&quot;user.password&quot;&gt;&lt;/TextField&gt;
593+
</code></pre>
594+
<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>
595+
<pre><code class="lang-JavaScript">import {Component} from &quot;angular2/core&quot;;
596+
import {User} from &quot;./shared/user/user&quot;;
597+
598+
@Component({
599+
selector: &quot;my-app&quot;,
600+
template: `
601+
&lt;StackLayout&gt;
602+
&lt;Image src=&quot;res://logo&quot; stretch=&quot;none&quot; horizontalAlignment=&quot;center&quot;&gt;&lt;/Image&gt;
603+
604+
&lt;TextField hint=&quot;Email Address&quot; keyboardType=&quot;email&quot; [(ngModel)]=&quot;user.email&quot;
605+
autocorrect=&quot;false&quot; autocapitalizationType=&quot;none&quot;&gt;&lt;/TextField&gt;
606+
&lt;TextField hint=&quot;Password&quot; secure=&quot;true&quot; [(ngModel)]=&quot;user.password&quot;&gt;&lt;/TextField&gt;
607+
608+
&lt;Button text=&quot;Sign in&quot; (tap)=&quot;signIn()&quot;&gt;&lt;/Button&gt;
609+
&lt;Button text=&quot;Sign up for Groceries&quot; class=&quot;link&quot;&gt;&lt;/Button&gt;
610+
&lt;/StackLayout&gt;
611+
`
612+
})
613+
export class AppComponent {
614+
user: User;
615+
constructor() {
616+
this.user = new User();
617+
}
618+
signIn() {
619+
alert(&quot;You’re using: &quot; + this.user.email);
620+
}
621+
}
622+
</code></pre>
623+
<div class="exercise-end"></div>
624+
562625
<h3 id="services">Services</h3>
563626
<h3 id="routing">Routing</h3>
564627

src/chapters/chapter3.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,86 @@ Even if you have no plans to create an Angular 2 web app, separating out your co
116116

117117
To see how this works in action let’s create a few files.
118118

119+
<h4 class="exercise-start">
120+
<b>Exercise</b>: Add a model object
121+
</h4>
122+
123+
The first thing we’ll do is add a model object to store user data. Open `app/user/user.service.ts` and paste in the following code:
124+
125+
``` JavaScript
126+
export class User {
127+
email: string;
128+
password: string;
129+
}
130+
```
131+
132+
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.
133+
134+
Next, open `app/app.component.ts`, and first add the following `import` to the top of the file:
135+
136+
``` JavaScript
137+
import {User} from "./shared/user/user";
138+
```
139+
140+
Here you import the `User` class that you just defined. Note the parallel between the `export` command used in the previous example and the `import` command used in the previous example. The reason the `User` class is available to import is because it was explicitly exported. You’ll see other examples of `import` and `export` as you go through this guide.
141+
142+
Next, replace the existing `AppComponent` definition with the one below, which uses the `User` class you just imported.
143+
144+
``` JavaScript
145+
export class AppComponent {
146+
user: User;
147+
constructor() {
148+
this.user = new User();
149+
}
150+
signIn() {
151+
alert("You’re using: " + this.user.email);
152+
}
153+
}
154+
```
155+
156+
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 also provide a TypeScript constructor, which is a function invoked when an instance of the `AppComponent` class is instantiated. In this app, Angular 2 instantiates an `AppComponent` for you with the call to `nativeScriptBootstrap(AppComponent)` done in `main.ts`.
157+
158+
Your final step is to use this new model object in your template. To do that, replace the existing two `<TextField>`s with the code shown below, which updates the `[(ngModel)]` bindings to point at the new `User` object:
159+
160+
``` XML
161+
<TextField hint="Email Address" keyboardType="email" [(ngModel)]="user.email"
162+
autocorrect="false" autocapitalizationType="none"></TextField>
163+
<TextField hint="Password" secure="true" [(ngModel)]="user.password"></TextField>
164+
```
165+
166+
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:
119167

168+
``` JavaScript
169+
import {Component} from "angular2/core";
170+
import {User} from "./shared/user/user";
171+
172+
@Component({
173+
selector: "my-app",
174+
template: `
175+
<StackLayout>
176+
<Image src="res://logo" stretch="none" horizontalAlignment="center"></Image>
177+
178+
<TextField hint="Email Address" keyboardType="email" [(ngModel)]="user.email"
179+
autocorrect="false" autocapitalizationType="none"></TextField>
180+
<TextField hint="Password" secure="true" [(ngModel)]="user.password"></TextField>
181+
182+
<Button text="Sign in" (tap)="signIn()"></Button>
183+
<Button text="Sign up for Groceries" class="link"></Button>
184+
</StackLayout>
185+
`
186+
})
187+
export class AppComponent {
188+
user: User;
189+
constructor() {
190+
this.user = new User();
191+
}
192+
signIn() {
193+
alert("You’re using: " + this.user.email);
194+
}
195+
}
196+
```
197+
198+
<div class="exercise-end"></div>
120199

121200
### Services
122201

0 commit comments

Comments
 (0)