You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<li><strong>CSS</strong>: You should know how to write simple CSS selectors, and know how to apply CSS rules as name/value pairs.</li>
59
59
<li><strong>The terminal</strong>: You should know how to open a terminal or command-line prompt on your development machine, how to change directories, and how to execute commands.</li>
60
60
</ul>
61
-
<p>This guide will <em>not</em>, however, assume you have any knowledge of Angular 2. When background Angular 2 expertise will help you understand a concept, this guide will link you to the appropriate places in the <ahref="https://angular.io/docs/ts/latest/">angular.io</a> documentation.</p>
61
+
<p>This guide will <em>not</em>, however, assume you have any knowledge of Angular 2 or TypeScript. When background Angular 2 or TypeScript expertise will help you understand a concept, this guide will link you to the appropriate places in the <ahref="https://angular.io/docs/ts/latest/">Angular</a> or <ahref="http://www.typescriptlang.org/Handbook">TypeScript</a> documentation.</p>
62
62
<p>With that out of the way, let’s get started!</p>
63
63
64
64
</div>
@@ -168,7 +168,7 @@ <h4 class="exercise-start">
168
168
<blockquote>
169
169
<p><strong>TIP</strong>: You can learn about how this is possible by reading more about <ahref="http://developer.telerik.com/featured/nativescript-works/">how NativeScript works</a>.</p>
170
170
</blockquote>
171
-
<p>To see livesync in action let’s make a small update to your app. Open your project’s <code>app/app.component.ts</code> file in your text editor of choice and change <code><Label text="hello world" /></code> to <code><Label text="hello NativeScript" /></code>. Save the file and you should see the app relaunch and the updated text displayed.</p>
171
+
<p>To see livesync in action let’s make a small update to your app. Open your project’s <code>app/app.component.ts</code> file in your text editor of choice and change <code><label text='hello world'></label></code> to <code><label text='hello NativeScript'></label></code>. Save the file and you should see the app relaunch and the updated text displayed.</p>
172
172
<divclass="exercise-end"></div>
173
173
174
174
<p>In addition to updating your NativeScript app on the fly, the livesync command also shows the output of <code>console.log()</code> statements as your app executes, as well as stack traces when things go wrong. So if your app crashes at any time during this guide, look to the terminal for a detailed report of the problem.</p>
</code></pre><p>Here's what these various files and folders do:</p>
208
209
<ul>
209
210
<li><strong>app</strong>: This folder contains all the development resources you need to build your app. You'll be spending most of your time editing the files in here.</li>
210
211
<li><strong>node_modules</strong>: This folder contains your app's npm module dependencies, including Angular 2, TypeScript, and the other modules NativeScript needs to build your app.</li>
211
-
<li><strong>node_modules/angular2</strong>: This folder contains the Angular 2 source code. NativeScript does not alter the core Angular 2 source code in any way.</li>
212
-
<li><strong>node_modules/nativescript-angular</strong>: This folder contains the module that builds on top of Angular 2 to integrate NativeScript-specific functionality. The source code for this module lives at <ahref="https://github.com/NativeScript/nativescript-angular">https://github.com/NativeScript/nativescript-angular</a>.</li>
213
-
<li><strong>node_modules/tns-core-modules</strong>: This folder contains your app's NativeScript modules, which are a series of NativeScript-provided JavaScript modules you'll use to build your app. Each module contains the platform-specific code needed to implement some feature—the camera, http calls, the file system, and so forth—exposed through a platform-agnostic API (e.g. <code>camera.takePicture()</code>). We'll look at some examples momentarily. The source code for these modules lives at <ahref="https://github.com/NativeScript/nativescript">https://github.com/NativeScript/nativescript</a>.</li>
214
-
<li><strong>package.json</strong>: This file contains your app's configuration details, such as your app id, the version of NativeScript you're using, and also which npm modules your app uses. We'll take a closer look at how to use this file when we talk about using npm modules in <ahref="#plugins-and-npm-modules">chapter 5</a>.</li>
212
+
<li><strong>node_modules/angular2</strong>: This folder contains the Angular 2 source code. NativeScript does not alter the core Angular 2 source code in any way, instead, NativeScript builds on top of Angular 2 with the nativescript-angular npm module.</li>
213
+
<li><strong>node_modules/nativescript-angular</strong>: This folder contains the module that integrates NativeScript-specific functionality into Angular 2. The source code for this module lives at <ahref="https://github.com/NativeScript/nativescript-angular">https://github.com/NativeScript/nativescript-angular</a>.</li>
214
+
<li><strong>node_modules/tns-core-modules</strong>: This folder contains your app's NativeScript modules, which are a series of NativeScript-provided JavaScript modules you'll use to build your app. Each module contains the platform-specific code needed to implement some feature—the camera, http calls, the file system, and so forth—exposed through a platform-agnostic API (e.g. <code>camera.takePicture()</code>). We'll look at some examples in <ahref="#chapter4">chapter 4</a>. The source code for these modules lives at <ahref="https://github.com/NativeScript/nativescript">https://github.com/NativeScript/nativescript</a>.</li>
215
215
<li><strong>platforms</strong>: This folder contains the platform-specific code NativeScript needs to build native iOS and Android apps. For example in the <code>android</code> folder you'll find things like your project's <code>AndroidManifest.xml</code> and .apk executable files. Similarly, the <code>ios</code> folder contains the Groceries' Xcode project and .ipa executables. Note, users on Windows machines will not have an <code>ios</code> folder.</li>
216
+
<li><strong>package.json</strong>: This file contains your app's configuration details, such as your app id, the version of NativeScript you're using, and also which npm modules your app uses. We'll take a closer look at how to use this file when we talk about using npm modules in <ahref="#plugins-and-npm-modules">chapter 5</a>.</li>
217
+
<li><strong>tsconfig.json</strong>: This file contains your app’s TypeScript configuration. Unless you have existing TypeScript expertise, you’ll probably want to leave this file alone for now. If you do have existing experience you may want to tweak these values to suit your personal preferences, however, note that the <code>"experimentalDecorators"</code> and <code>"emitDecoratorMetadata"</code> flags are essential to making NativeScript and Angular 2 work, so don’t remove those. You can refer to the official TypeScript wiki for <ahref="https://github.com/Microsoft/TypeScript/wiki/tsconfig.json">detailed documentation on what you can do in a <code>tsconfig.json</code> file</a>.</li>
216
218
</ul>
217
219
<p>The NativeScript CLI manages the <code>platforms</code> folder for you as you develop and run your app; therefore, it's a best practice to treat the <code>platforms</code> folder as generated code. The Groceries app includes the <code>platforms</code> folder in its <ahref="https://github.com/NativeScript/sample-Groceries/blob/master/.gitignore"><code>.gitignore</code></a> to exclude its files from source control.</p>
218
220
<p>Next, let's dig into the <code>app</code> folder, as that's where you'll be spending the majority of your time.</p>
</code></pre><p>Here's what these various files and folders do:</p>
237
238
<ul>
238
239
<li><strong>App_Resources</strong>: This folder contains platform-specific resources such as icons, splash screens, and configuration files. The NativeScript CLI takes care of injecting these resources into the appropriate places in the <code>platforms</code> folder when you execute <code>tns run</code>.</li>
239
-
<li><strong>shared</strong>: This folder, specific to the Groceries app, contains any files you need to share across views in your app. In the Groceries app, you'll find a few view model objects and a <code>config.js</code> file used to share configuration variables like API keys.</li>
240
-
<li><strong>views</strong>: This folder contains the code to build your app's views, each of which will have a subfolder in <code>views</code>. Each view is made up of an XML file, a JavaScript file, and an optional CSS file. The groceries app contains three folders for its three views.</li>
240
+
<li><strong>pages</strong>: This folder, specific to the Groceries app, contains the code to build your app's pages. Each page is made up of a TypeScript file and an optional HTML file. The Groceries app starts with three TypeScript files for its three pages—a login page, a registration page, and a list page. In a more complex app you may wish to create a more detailed folder structure within <code>pages</code>, but for now, keeping all pages in the root simplifies our demo.</li>
241
+
<li><strong>shared</strong>: This folder, also specific to the Groceries app, contains any files you need to share across pages in your app. In the Groceries app, you'll find a few service classes for talking to backend services, a few model objects, and a <code>config.ts</code>file used to share configuration variables like API keys.</li>
241
242
<li><strong>app.css</strong>: This file contains global styles for your app. We'll dig into app styling in <ahref="#css">section 2.3</a>.</li>
242
-
<li><strong>app.js</strong>: This file sets up your application's starting module and initializes the app.</li>
243
+
<li><strong>app.component.ts</strong>: This primary Angular component that drives your application. Eventually this file will handle routing and application-wide configuration, however for now the file has a simple hello world example that we’ll look at momentarily.</li>
244
+
<li><strong>main.ts</strong>: The starting point of Angular 2 applications—web and native.</li>
243
245
</ul>
244
-
<p>Let's start with <code>app/app.js</code>, as it's the starting point for NativeScript apps. Your <code>app.js</code> contains the three lines below: </p>
<p>To get a sense of a NativeScript app actually starts up, let’s dig in to the first few files.</p>
247
+
<h3id="starting-up">Starting up</h3>
248
+
<p>The first few files you run in a NativeScript app look almost identical to <ahref="https://angular.io/docs/ts/latest/quickstart.html">the first few files you run in an Angular 2 web app</a>. Let’s start with <code>main.ts</code> as that’s the first file executed. Open your <code>app.main.ts</code> file; you should see the code below:</p>
249
+
<pre><codeclass="lang-JavaScript">import {nativeScriptBootstrap} from "nativescript-angular/application";
250
+
import {AppComponent} from "./app.component";
251
+
252
+
nativeScriptBootstrap(AppComponent);
248
253
</code></pre>
249
-
<p>Here, you're requiring, or importing, the <ahref="{{site.baseurl}}/ApiReference/application/HOW-TO">NativeScript application module</a>. Then, you set its <code>mainModule</code>, or the starting screen of your app to be the login screen, which lives in your app's <code>views/login</code>folder.</p>
254
+
<p>Here you’re using the TypeScript <code>import</code> command to bring in a function—<code>nativescriptBootstrap()</code>—and a <ahref="http://www.typescriptlang.org/Handbook#classes">TypeScript class</a>—<code>AppComponent</code>—each of which are defined in separate files. The <code>nativescriptBootstrap()</code> function comes from the “nativescript-angular” npm module, which you may recall contains the code needed to integrate NativeScript and Angular 2. Whereas <ahref="https://angular.io/docs/ts/latest/api/platform/browser/bootstrap-function.html">Angular 2’s own <code>bootstrap()</code>function</a> starts an Angular 2 browser app, NativeScript’s bootstrap function starts an Angular 2 native app.</p>
250
255
<blockquote>
251
-
<p><strong>TIP</strong>: JavaScript modules in NativeScript follow the <ahref="http://wiki.commonjs.org/wiki/CommonJS">CommonJS specification</a>. This means you can use the <ahref="http://wiki.commonjs.org/wiki/Modules/1.1#Module_Context"><code>require()</code>method</a> to import modules, as is done above, as well as use the <code>export</code> keyword to expose a module's properties and methods, which we'll look at later in this chapter. These are the same constructs Node.js uses for JavaScript modules, so if you know how to use Node.js modules, you already know how to use NativeScript modules.</p>
256
+
<p><strong>TIP</strong>: If you’re curious about what <code>nativescriptBootstrap()</code>function actually has to do to startup native iOS and Android apps, remember that all this code is open source for you to explore at any time. The <code>nativescriptBootstrap()</code> function specifically is defined in an <ahref="https://github.com/NativeScript/nativescript-angular/blob/master/src/nativescript-angular/application.ts"><code>application.ts</code> file</a> in the <ahref="https://github.com/NativeScript/nativescript-angular">NativeScript/nativescript-angular repository</a> on GitHub.</p>
252
257
</blockquote>
253
-
<p>Now that your app is ready for development, let's add some UI components to make your login screen show more than some basic text.</p>
258
+
<p>The bootstrap function, regardless of whether it’s for the web or for native apps, needs to know which Angular component to start the application with. In this case, you’re passing to control to a <code>AppComponent</code> component defined in <code>app.component.ts</code>.</p>
259
+
<blockquote>
260
+
<p><strong>TIP</strong>: In NativeScript we follow Angular 2’s own convention of naming component files with a <code>.component.ts</code> suffix.</p>
261
+
</blockquote>
262
+
<p>Next, open your app’s <code>app/app.component.ts</code> file; you should see the code below:</p>
263
+
<pre><codeclass="lang-JavaScript">// TODO: I can’t explain why this is necessary until I know why this is necessary
264
+
// import "reflect-metadata";
265
+
import {Component} from "angular2/core";
266
+
267
+
@Component({
268
+
// TODO: Can I just remove this to simplify the explanation below? Is it really “my” app if I cloned the app from GitHub? WHAT DOES IT ALL MEAN?
<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>
276
+
<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>
277
+
<p>Notice the interesting way that the <code>Component</code> class is used—with the syntax <code>@Component</code>. This is a <ahref="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><label text="hello NativeScript"></label></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><label></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>
Copy file name to clipboardExpand all lines: src/chapters/chapter0.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,6 @@ This guide assumes that you have some basic knowledge of JavaScript, CSS, and yo
39
39
***CSS**: You should know how to write simple CSS selectors, and know how to apply CSS rules as name/value pairs.
40
40
***The terminal**: You should know how to open a terminal or command-line prompt on your development machine, how to change directories, and how to execute commands.
41
41
42
-
This guide will _not_, however, assume you have any knowledge of Angular 2. When background Angular 2 expertise will help you understand a concept, this guide will link you to the appropriate places in the [angular.io](https://angular.io/docs/ts/latest/) documentation.
42
+
This guide will _not_, however, assume you have any knowledge of Angular 2 or TypeScript. When background Angular 2 or TypeScript expertise will help you understand a concept, this guide will link you to the appropriate places in the [Angular](https://angular.io/docs/ts/latest/) or [TypeScript](http://www.typescriptlang.org/Handbook) documentation.
Copy file name to clipboardExpand all lines: src/chapters/chapter1.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -156,7 +156,7 @@ The `tns livesync` command updates your app by transferring the updated source c
156
156
157
157
> **TIP**: You can learn about how this is possible by reading more about [how NativeScript works](http://developer.telerik.com/featured/nativescript-works/).
158
158
159
-
To see livesync in action let’s make a small update to your app. Open your project’s `app/app.component.ts` file in your text editor of choice and change `<Label text="hello world" />` to `<Label text="hello NativeScript" />`. Save the file and you should see the app relaunch and the updated text displayed.
159
+
To see livesync in action let’s make a small update to your app. Open your project’s `app/app.component.ts` file in your text editor of choice and change `<label text='hello world'></label>` to `<label text='hello NativeScript'></label>`. Save the file and you should see the app relaunch and the updated text displayed.
0 commit comments