Skip to content

Commit 59d197d

Browse files
committed
Finishing 3.0 on events
1 parent bac9762 commit 59d197d

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

images/chapter3/terminal-1.png

11.1 KB
Loading

index.html

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,29 @@ <h4 class="exercise-start">
474474
</div>
475475
<div class="chapter">
476476
<h2 id="application-logic">Application Logic</h2>
477-
<h3 id="events-and-data-binding">Events and data binding</h3>
477+
<p>In this chapter you’ll learn how to add JavaScript logic to your app, how to create services that talk to backend endpoints, and how to architect an app that uses multiple pages. There’s a lot to cover, so let’s start by discussing how to handle events and data binding.</p>
478+
<h3 id="events">Events</h3>
479+
<p>Most user interfaces are driven by events. In NativeScript apps, those events are usually some user action, such as tapping, swiping, or rotating—and NativeScript abstracts the iOS- and Android-specific code for handling such events into a handful of easy-to-use APIs. Let’s start with the most common event you’ll use in a NativeScript app—<code>tap</code>.</p>
480+
<h4 class="exercise-start">
481+
<b>Exercise</b>: Add a <code>tap</code> event handler
482+
</h4>
483+
484+
<p>Open <code>app/app.component.ts</code>, find the existing sign in button within your component’s <code>template</code> (<code>&lt;Button text=&quot;Sign in&quot;&gt;&lt;/Button&gt;</code>), and replace it with the following code:</p>
485+
<pre><code class="lang-JavaScript">&lt;Button text=&quot;Sign in&quot; (tap)=&quot;signIn()&quot;&gt;&lt;/Button&gt;
486+
</code></pre>
487+
<p>Next, in the same file, replace the current <code>AppComponent</code> definition with the one shown below:</p>
488+
<pre><code class="lang-JavaScript">export class AppComponent {
489+
signIn() {
490+
console.log(&quot;hello&quot;);
491+
}
492+
}
493+
</code></pre>
494+
<div class="exercise-end"></div>
495+
496+
<p>The <code>(eventName)=&quot;functionName()&quot;</code> syntax is part of <a href="https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding">Angular 2’s event binding system</a>, which lets you bind an event that occurs on a UI element to a function in your component’s class. In this case, the <code>(tap)=&quot;signIn()&quot;</code> syntax you just used tells Angular to run the <code>AppComponent</code> class’s <code>signIn()</code> function whenever the user taps the sign in button.</p>
497+
<p>To verify this binding works you can try tapping the sign in button now; you should see “hello” logged in your terminal or command prompt as is shown below:</p>
498+
<p><img src="images/chapter3/terminal-1.png" alt="Terminal showing the word hello logged"></p>
499+
<h3 id="data-binding">Data Binding</h3>
478500
<h3 id="services">Services</h3>
479501
<h3 id="routing">Routing</h3>
480502

src/chapters/chapter3.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
## Application Logic
22

3-
### Events and data binding
3+
In this chapter you’ll learn how to add JavaScript logic to your app, how to create services that talk to backend endpoints, and how to architect an app that uses multiple pages. There’s a lot to cover, so let’s start by discussing how to handle events and data binding.
4+
5+
### Events
6+
7+
Most user interfaces are driven by events. In NativeScript apps, those events are usually some user action, such as tapping, swiping, or rotating—and NativeScript abstracts the iOS- and Android-specific code for handling such events into a handful of easy-to-use APIs. Let’s start with the most common event you’ll use in a NativeScript app—`tap`.
8+
9+
<h4 class="exercise-start">
10+
<b>Exercise</b>: Add a `tap` event handler
11+
</h4>
12+
13+
Open `app/app.component.ts`, find the existing sign in button within your component’s `template` (`<Button text="Sign in"></Button>`), and replace it with the following code:
14+
15+
``` JavaScript
16+
<Button text="Sign in" (tap)="signIn()"></Button>
17+
```
18+
19+
Next, in the same file, replace the current `AppComponent` definition with the one shown below:
20+
21+
``` JavaScript
22+
export class AppComponent {
23+
signIn() {
24+
console.log("hello");
25+
}
26+
}
27+
```
28+
29+
<div class="exercise-end"></div>
30+
31+
The `(eventName)="functionName()"` syntax is part of [Angular 2’s event binding system](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding), which lets you bind an event that occurs on a UI element to a function in your component’s class. In this case, the `(tap)="signIn()"` syntax you just used tells Angular to run the `AppComponent` class’s `signIn()` function whenever the user taps the sign in button.
32+
33+
To verify this binding works you can try tapping the sign in button now; you should see “hello” logged in your terminal or command prompt as is shown below:
34+
35+
![Terminal showing the word hello logged](images/chapter3/terminal-1.png)
36+
37+
### Data Binding
438

539
### Services
640

0 commit comments

Comments
 (0)