Skip to content

Commit 78c4a14

Browse files
committed
Initial version of chapter 2.2
1 parent 334c8c9 commit 78c4a14

File tree

4 files changed

+117
-20
lines changed

4 files changed

+117
-20
lines changed

index.html

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ <h3 id="what-you-re-building">What you&#39;re building</h3>
4444
<li>Runs cross-platform (iOS and Android).</li>
4545
</ul>
4646
<p>If you follow along to the end, here&#39;s what the finished app will look like on iOS:</p>
47-
<p><img src="/images/chapter0/ios/1.png" alt="login">
48-
<img src="/images/chapter0/ios/2.png" alt="register">
49-
<img src="/images/chapter0/ios/3.png" alt="list"></p>
47+
<p><img src="images/chapter0/ios/1.png" alt="login">
48+
<img src="images/chapter0/ios/2.png" alt="register">
49+
<img src="images/chapter0/ios/3.png" alt="list"></p>
5050
<p>And here&#39;s what the app will look like on Android:</p>
51-
<p><img src="/images/chapter0/android/1.png" alt="">
52-
<img src="/images/chapter0/android/2.png" alt="">
53-
<img src="/images/chapter0/android/3.png" alt=""></p>
51+
<p><img src="images/chapter0/android/1.png" alt="">
52+
<img src="images/chapter0/android/2.png" alt="">
53+
<img src="images/chapter0/android/3.png" alt=""></p>
5454
<h3 id="prerequisites">Prerequisites</h3>
5555
<p>This guide assumes that you have some basic knowledge of JavaScript, CSS, and your development machine’s terminal. More specifically:</p>
5656
<ul>
@@ -131,7 +131,7 @@ <h4 class="exercise-start">
131131
<p>If you&#39;re on a Mac, start by running the app in an iOS simulator with the following command:</p>
132132
<pre><code>tns run ios --emulator
133133
</code></pre><p>If all went well, you should see something like this:</p>
134-
<p><img src="/images/chapter1/ios/1.png" alt="iOS login"></p>
134+
<p><img src="images/chapter1/ios/1.png" alt="iOS login"></p>
135135
<p>Next, run your app on an Android emulator with the following command:</p>
136136
<pre><code>tns run android --emulator
137137
</code></pre><blockquote>
@@ -142,7 +142,7 @@ <h4 class="exercise-start">
142142
</ul>
143143
</blockquote>
144144
<p>If all went well, you should see your app running in an Android emulator:</p>
145-
<p><img src="/images/chapter1/android/1.png" alt="Android login"></p>
145+
<p><img src="images/chapter1/android/1.png" alt="Android login"></p>
146146
<div class="exercise-end"></div>
147147

148148
<p>Here are a few other tips for running NativeScript apps.</p>
@@ -275,8 +275,56 @@ <h3 id="starting-up">Starting up</h3>
275275
<p>This file contains an Angular 2 component, which is the primary building block you’ll use to build your NativeScript applications. Let’s break down what’s going on in this file.</p>
276276
<p>First, you again use TypeScript’s <code>import</code> command to bring in externally defined functionality—in this case, the <code>Component</code> class from Angular 2 itself. In Angular 2 a component manages a view, or a piece of the user interface that the user sees. A component be used to define an individual UI element, or an entire page, and eventually we’ll add a bunch of logic to these components and use them to build an entire app. But for now this component is simple for the purpose of demonstration.</p>
277277
<p>Notice the interesting way that the <code>Component</code> class is used—with the syntax <code>@Component</code>. This is a <a href="https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Decorators.md">TypeScript decorator</a>, which allows you to annotate a TypeScript class or method with additional information. For now, you can think of it as a way of adding some metadata configuration to the currently empty <code>AppComponent</code> class. Specifically, the <code>@Component</code> decorator’s <code>template</code> property tells NativeScript how to render this component on the screen. In fact, the <code>&lt;label text=&quot;hello NativeScript&quot;&gt;&lt;/label&gt;</code> syntax is why you saw “hello NativeScript” when you ran this app earlier.</p>
278-
<p>However, this syntax may look a bit odd if you come from a web development background. On the web, the <code>&lt;label&gt;</code> HTML element doesn’t have a <code>text</code> attribute, so what’s going on here. Let’s dive into this by looking at how NativeScript UI components work.</p>
279-
<h3 id="adding-ui-components">Adding UI components</h3>
278+
<p>However, this syntax may look a bit odd if you come from a web development background. On the web, the <code>&lt;label&gt;</code> HTML element doesn’t have a <code>text</code> attribute, so what’s going on here. Let’s dive into this by looking at how NativeScript UI elements work.</p>
279+
<h3 id="adding-ui-elements">Adding UI elements</h3>
280+
<p>The primary difference between building an Angular 2 app for the web and an Angular 2 app with NativeScript is in the UI elements that you use. NativeScript apps do not use a browser and do not have a DOM; therefore, elements like <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> simply do not work.</p>
281+
<p>No worries though, as NativeScript provides an <a href="http://docs.nativescript.org/ui/ui-views">extensive suite of UI elements</a>, each of which are implemented with native iOS and Android controls. For instance, the <a href="http://docs.nativescript.org/ui/ui-views#label"><code>&lt;label&gt;</code> control</a> our previous example used is actually rendered as a <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/"><code>UILabel</code></a> on iOS and an <a href="http://developer.android.com/reference/android/widget/TextView.html"><code>android.widget.TextView</code></a> on Android. The great thing about using NativeScript though, is that this native details are transparent to use as a developer. You type <code>&lt;label&gt;</code> and let NativeScript handle the rendering details.</p>
282+
<p>Let’s return back to building Groceries. The first screen of Groceries is intended to be a login screen, so let’s replace the current <code>&lt;label&gt;</code> with something that resembles your typical login screen in a mobile app.</p>
283+
<h4 class="exercise-start">
284+
<b>Exercise</b>: Add UI elements to <code>app.component.ts</code>
285+
</h4>
286+
287+
<p>Open <code>app/app.component.ts</code> and replace the existing <code>@Component</code> with the following code:</p>
288+
<pre><code class="lang-JavaScript">@Component({
289+
// TODO: Is this necessary?
290+
// selector: &quot;my-app&quot;,
291+
template: `
292+
&lt;text-field hint=&quot;Email Address&quot; keyboardType=&quot;email&quot;
293+
autocorrect=&quot;false&quot; autocapitalizationType=&quot;none&quot;&gt;&lt;/text-field&gt;
294+
&lt;text-field hint=&quot;Password&quot; secure=&quot;true&quot;&gt;&lt;/text-field&gt;
295+
296+
&lt;button text=&quot;Sign in&quot;&gt;&lt;/button&gt;
297+
&lt;button text=&quot;Sign up for Groceries&quot;&gt;&lt;/button&gt;
298+
`
299+
})
300+
</code></pre>
301+
<div class="exercise-end"></div>
302+
303+
<blockquote>
304+
<p><strong>NOTE</strong>: Notice the back-tick character (`) used with the <code>template</code> property. This character is used to define an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">ES2015 template literal</a>, which TypeScript supports, and which allows you to write multi-line strings without using messy string concatenation.</p>
305+
</blockquote>
306+
<p>This code adds two new NativeScript UI elements: a <a href="http://docs.nativescript.org/ApiReference/ui/text-field/how-to.html">text field</a> and a <a href="http://docs.nativescript.org/ApiReference/ui/button/how-to.html">button</a>. Much like HTML elements, NativeScript UI elements provide attributes to let you configure their behavior and appearance. The code you just added uses the following attributes:</p>
307+
<ul>
308+
<li><code>&lt;text-field&gt;</code><ul>
309+
<li><code>hint</code>: Shows placeholder text that tells the user what to type.</li>
310+
<li><code>keyboardType</code>: The type of keyboard to present to the user for input. <code>keyboardType=&quot;email&quot;</code> shows a keyboard optimized for entering email addresses. NativeScript currently supports <a href="http://docs.nativescript.org/ui/keyboard.html">five types of keyboards</a> for text fields.</li>
311+
<li><code>autocorrect</code>: A boolean attribute that determines whether the mobile operating system should autocorrect user input. In the case of email address text fields, the autocorrect behavior is undesirable.</li>
312+
<li><code>autocapitalizationType</code>: Determines how the operating system should autocapitalize user input. <code>autocapitalizationType=&quot;none&quot;</code> turns autocapitalization off altogether. NativeScript supports <a href="{{site.baseurl}}/ApiReference/ui/enums/AutocapitalizationType/README.html">four autocapitalization types</a> on text fields.</li>
313+
<li><code>secure</code>: A boolean attribute that determines whether the TextField&#39;s text should be masked, which is commonly done on password fields.</li>
314+
</ul>
315+
</li>
316+
<li><code>&lt;button&gt;</code><ul>
317+
<li><code>text</code>: Controls the text displayed within the button.</li>
318+
</ul>
319+
</li>
320+
</ul>
321+
<p>After you <a href="#development-workflow">run your app</a> you may expect to see a polished login screen, but instead you will see a single <code>&lt;Button&gt;</code> component on the screen:</p>
322+
<p><img src="images/chapter2/ios/1.png" alt="login 1">
323+
<img src="images/chapter2/android/1.png" alt="login 1"></p>
324+
<p>The reason for this is you you need to tell NativeScript how to layout your page’s UI elements. Let&#39;s look at how to use NativeScript layouts to arrange these components on the screen.</p>
325+
<blockquote>
326+
<p><strong>TIP</strong>: The NativeScript docs include a <a href="{{site.baseurl}}/ui-with-xml">full list of the UI components and attributes</a> with which you can build your apps. You can even <a href="{{site.baseurl}}/ui-with-xml#custom-components">build your own, custom UI components</a>.</p>
327+
</blockquote>
280328

281329
</div>
282330
</div>

src/chapters/chapter0.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ This guide will walk you through building [Groceries](https://github.com/NativeS
2121

2222
If you follow along to the end, here's what the finished app will look like on iOS:
2323

24-
![login](/images/chapter0/ios/1.png)
25-
![register](/images/chapter0/ios/2.png)
26-
![list](/images/chapter0/ios/3.png)
24+
![login](images/chapter0/ios/1.png)
25+
![register](images/chapter0/ios/2.png)
26+
![list](images/chapter0/ios/3.png)
2727

2828
And here's what the app will look like on Android:
2929

30-
![](/images/chapter0/android/1.png)
31-
![](/images/chapter0/android/2.png)
32-
![](/images/chapter0/android/3.png)
30+
![](images/chapter0/android/1.png)
31+
![](images/chapter0/android/2.png)
32+
![](images/chapter0/android/3.png)
3333

3434
### Prerequisites
3535

src/chapters/chapter1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ tns run ios --emulator
106106

107107
If all went well, you should see something like this:
108108

109-
![iOS login](/images/chapter1/ios/1.png)
109+
![iOS login](images/chapter1/ios/1.png)
110110

111111
Next, run your app on an Android emulator with the following command:
112112

@@ -120,7 +120,7 @@ tns run android --emulator
120120
121121
If all went well, you should see your app running in an Android emulator:
122122

123-
![Android login](/images/chapter1/android/1.png)
123+
![Android login](images/chapter1/android/1.png)
124124

125125
<div class="exercise-end"></div>
126126

src/chapters/chapter2.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,57 @@ First, you again use TypeScript’s `import` command to bring in externally defi
111111

112112
Notice the interesting way that the `Component` class is used—with the syntax `@Component`. This is a [TypeScript decorator](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Decorators.md), which allows you to annotate a TypeScript class or method with additional information. For now, you can think of it as a way of adding some metadata configuration to the currently empty `AppComponent` class. Specifically, the `@Component` decorator’s `template` property tells NativeScript how to render this component on the screen. In fact, the `<label text="hello NativeScript"></label>` syntax is why you saw “hello NativeScript” when you ran this app earlier.
113113

114-
However, this syntax may look a bit odd if you come from a web development background. On the web, the `<label>` HTML element doesn’t have a `text` attribute, so what’s going on here. Let’s dive into this by looking at how NativeScript UI components work.
114+
However, this syntax may look a bit odd if you come from a web development background. On the web, the `<label>` HTML element doesn’t have a `text` attribute, so what’s going on here. Let’s dive into this by looking at how NativeScript UI elements work.
115115

116-
### Adding UI components
116+
### Adding UI elements
117117

118+
The primary difference between building an Angular 2 app for the web and an Angular 2 app with NativeScript is in the UI elements that you use. NativeScript apps do not use a browser and do not have a DOM; therefore, elements like `<div>` and `<span>` simply do not work.
118119

120+
No worries though, as NativeScript provides an [extensive suite of UI elements](http://docs.nativescript.org/ui/ui-views), each of which are implemented with native iOS and Android controls. For instance, the [`<label>` control](http://docs.nativescript.org/ui/ui-views#label) our previous example used is actually rendered as a [`UILabel`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/) on iOS and an [`android.widget.TextView`](http://developer.android.com/reference/android/widget/TextView.html) on Android. The great thing about using NativeScript though, is that this native details are transparent to use as a developer. You type `<label>` and let NativeScript handle the rendering details.
121+
122+
Let’s return back to building Groceries. The first screen of Groceries is intended to be a login screen, so let’s replace the current `<label>` with something that resembles your typical login screen in a mobile app.
123+
124+
<h4 class="exercise-start">
125+
<b>Exercise</b>: Add UI elements to <code>app.component.ts</code>
126+
</h4>
127+
128+
Open `app/app.component.ts` and replace the existing `@Component` with the following code:
129+
130+
``` JavaScript
131+
@Component({
132+
// TODO: Is this necessary?
133+
// selector: "my-app",
134+
template: `
135+
<text-field hint="Email Address" keyboardType="email"
136+
autocorrect="false" autocapitalizationType="none"></text-field>
137+
<text-field hint="Password" secure="true"></text-field>
138+
139+
<button text="Sign in"></button>
140+
<button text="Sign up for Groceries"></button>
141+
`
142+
})
143+
```
144+
145+
<div class="exercise-end"></div>
146+
147+
> **NOTE**: Notice the back-tick character (\`) used with the `template` property. This character is used to define an [ES2015 template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals), which TypeScript supports, and which allows you to write multi-line strings without using messy string concatenation.
148+
149+
This code adds two new NativeScript UI elements: a [text field](http://docs.nativescript.org/ApiReference/ui/text-field/how-to.html) and a [button](http://docs.nativescript.org/ApiReference/ui/button/how-to.html). Much like HTML elements, NativeScript UI elements provide attributes to let you configure their behavior and appearance. The code you just added uses the following attributes:
150+
151+
- `<text-field>`
152+
- `hint`: Shows placeholder text that tells the user what to type.
153+
- `keyboardType`: The type of keyboard to present to the user for input. `keyboardType="email"` shows a keyboard optimized for entering email addresses. NativeScript currently supports [five types of keyboards](http://docs.nativescript.org/ui/keyboard.html) for text fields.
154+
- `autocorrect`: A boolean attribute that determines whether the mobile operating system should autocorrect user input. In the case of email address text fields, the autocorrect behavior is undesirable.
155+
- `autocapitalizationType`: Determines how the operating system should autocapitalize user input. `autocapitalizationType="none"` turns autocapitalization off altogether. NativeScript supports [four autocapitalization types]({{site.baseurl}}/ApiReference/ui/enums/AutocapitalizationType/README.html) on text fields.
156+
- `secure`: A boolean attribute that determines whether the TextField's text should be masked, which is commonly done on password fields.
157+
- `<button>`
158+
- `text`: Controls the text displayed within the button.
159+
160+
After you [run your app](#development-workflow) you may expect to see a polished login screen, but instead you will see a single `<Button>` component on the screen:
161+
162+
![login 1](images/chapter2/ios/1.png)
163+
![login 1](images/chapter2/android/1.png)
164+
165+
The reason for this is you you need to tell NativeScript how to layout your page’s UI elements. Let's look at how to use NativeScript layouts to arrange these components on the screen.
166+
167+
> **TIP**: The NativeScript docs include a [full list of the UI components and attributes]({{site.baseurl}}/ui-with-xml) with which you can build your apps. You can even [build your own, custom UI components]({{site.baseurl}}/ui-with-xml#custom-components).

0 commit comments

Comments
 (0)