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
<p><strong>TIP</strong>: There are other ways to share code between native and web apps besides the <code>shared</code> folder convention Groceries uses. For an approach that places web and native code in the same codebase, that also provides some additional tooling around testing and internationalization, check out <ahref="https://github.com/NathanWalker/angular2-seed-advanced">Nathan Walker’s advanced Angular 2 seed project</a>.</p>
1070
1070
</blockquote>
1071
-
<p>And we’re just getting started. NativeScript provides a number of other ways to tie into native device functionality out of the box through NativeScript modules. Let’s look at how they work.</p>
1071
+
<p>Now that we’ve discussed how NativeScript apps and Angular 2 web apps are similar, let’s move onto how they’re different. When you’re building with NativeScript you have access to the entirety of iOS and Android, as well as the highly capable devices they run on—enabling you to build rich experiences that go above and beyond what the web can offer.</p>
1072
+
<p>The best part is, one NativeScript feature makes it trivial to tie into this native device functionality: NativeScript modules.</p>
<p>In this chapter you'll learn about NativeScript modules, which are the JavaScript modules in your app's <code>node_modules/tns-core-modules</code> folder. Whether you've realized it or not, you've already used several NativeScript modules, as all of the NativeScript UI elements are actually implemented with JavaScript code.</p>
1077
+
<p>In this chapter you'll learn about NativeScript modules, which are the TypeScript modules in your app's <code>node_modules/tns-core-modules</code> folder. Whether you've realized it or not, you've already used several NativeScript modules, as all of the NativeScript UI elements are actually implemented with TypeScript code.</p>
1077
1078
<p>If you dig into <code>node_modules/tns-core-modules</code> you can get an idea of how these modules work. Start by finding the <code>node_modules/tns-core-modules/camera</code> folder, which includes the implementation of the camera module. It includes:</p>
1078
1079
<ul>
1079
1080
<li>a <code>package.json</code> file that sets the name of the module;</li>
<li>a file containing code shared by the Android and iOS implementations (<code>camera-common.js</code>)</li>
1083
1084
</ul>
1084
1085
<blockquote>
1085
-
<p><strong>NOTE</strong>: You can refer to the <ahref="https://nodejs.org/api/modules.html#modules_folders_as_modules">Node.js documentation on folders as modules</a> for more detailed information on how NativeScript organizes its modules.</p>
1086
+
<p><strong>NOTE</strong>:</p>
1087
+
<ul>
1088
+
<li>You can refer to the <ahref="https://nodejs.org/api/modules.html#modules_folders_as_modules">Node.js documentation on folders as modules</a> for more detailed information on how NativeScript organizes its modules.</li>
1089
+
<li>The “tns-core-modules” package only includes compiled JavaScript code to cut down on file size. You can find the TypeScript code for each of these modules in the `<ahref="https://github.com/NativeScript/nativescript">main NativeScript GitHub repo</a>, for instance here’s the <ahref="https://github.com/NativeScript/NativeScript/tree/master/camera">camera module’s source code</a>.</li>
1090
+
</ul>
1086
1091
</blockquote>
1087
1092
<p>The <code>*.ios.*</code> and <code>*.android.*</code> naming convention should look familiar, as it’s the exact same convention we used to include Android- and iOS-specific styling in <ahref="#css">chapter 2.3</a>. NativeScript uses this same convention to implement its modules on iOS and Android. Now that you know where these modules are, let's take a closer look at what else they can do for your app, starting with a closer looks at what you can do with NativeScript’s UI elements.</p>
<pre><codeclass="lang-TypeScript">import {topmost} from "ui/frame";
1096
1101
import {Page} from "ui/page";
1097
1102
</code></pre>
1103
+
<blockquote>
1104
+
<p><strong>NOTE</strong>: All of the imports you’ve seen to this point work because the TypeScript compiler resolves them against your project’s <code>node_modules</code> folder. For instance, <code>import {Component} from "angular2/core"</code> works because a <code>node_modules/angular2/core.d.ts</code> file exists. The two imports above are NativeScript module imports, and they work because your project’s <code>references.d.ts</code> file includes a reference to a TypeScript declaration file (a <code>.d.ts</code> file), that lives in <code>node_modules/tns-core-modules</code>, and which allows you to import modules from <code>node_modules/tns-core-modules</code> without any prefixes.</p>
1105
+
</blockquote>
1098
1106
<p>Next, alter the same file’s existing <code>"angular2/core"</code> import to include the <code>OnInit</code> interface:</p>
1099
1107
<pre><codeclass="lang-TypeScript">import {Component, OnInit} from "angular2/core";
1100
1108
</code></pre>
1101
1109
<p><code>OnInit</code> is a <ahref="http://www.typescriptlang.org/docs/handbook/interfaces.html#class-types">TypeScript class interface</a>. To see how it works, make the following change to the declaration of your <code>LoginPage</code> class:</p>
1102
1110
<pre><codeclass="lang-TypeScript">export class LoginPage implements OnInit {
1103
1111
</code></pre>
1104
-
<p>If you’re using an editor that supports TypeScript, you should see an error that says something like <em>“Class ‘LoginPage’ incorrectly implements interface ‘OnInit’”</em>. When you implement a TypeScript class interface, you’re telling the TypeScript compiler that you will implement any of the methods that the interface requires. In the case of <code>OnInit</code>, Angular 2 requires you to implement a single <code>ngOnInit()</code> method. To implement it, add the following code within the <code>LoginPage</code> class:</p>
1112
+
<p>If you’re using an editor that supports TypeScript, you should see an error that says something like <em>“Class ‘LoginPage’ incorrectly implements interface ‘OnInit’”</em>. When you implement a TypeScript class interface, you’re telling the TypeScript compiler that you must implement all methods that the interface requires. In the case of <code>OnInit</code>, Angular 2 requires you to implement a single <code>ngOnInit()</code> method. To implement it, add the following code within the <code>LoginPage</code> class:</p>
<p><strong>NOTE</strong>: The <code>this.page.ios</code> in this function can go away after NativeScript 2.0 is released. The check is there because of <ahref="https://github.com/NativeScript/NativeScript/issues/1788">this bug</a>.</p>
1120
+
<p><strong>NOTE</strong>: The <code>this.page.ios</code>check in this function can go away after NativeScript 2.0 is released. The check is there because of <ahref="https://github.com/NativeScript/NativeScript/issues/1788">this bug</a>.</p>
1113
1121
</blockquote>
1114
1122
<p><code>ngOnInit</code> is one of several <ahref="https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html">component lifecycle hooks</a> that Angular 2 provides. As its name implies, <code>ngOnInit</code> gets invoked when Angular initializes this component.</p>
1115
-
<p>We’ll discuss what the code within <code>ngOnInit()</code> does momentarily, but finally, to make these changes compile and run, add the following property to the <code>LoginPage</code> class. (You can put it right under the <code>user: User;</code> line.)</p>
1123
+
<p>We’ll discuss what the code within <code>ngOnInit()</code> does momentarily, but finally, to make these changes compile and run, add the following property to the <code>LoginPage</code> class. You can put it right under the <code>user: User;</code> line.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -652,4 +652,6 @@ The power of NativeScript is you have the ability to use the same Angular conven
652
652
653
653
> **TIP**: There are other ways to share code between native and web apps besides the `shared` folder convention Groceries uses. For an approach that places web and native code in the same codebase, that also provides some additional tooling around testing and internationalization, check out [Nathan Walker’s advanced Angular 2 seed project](https://github.com/NathanWalker/angular2-seed-advanced).
654
654
655
-
And we’re just getting started. NativeScript provides a number of other ways to tie into native device functionality out of the box through NativeScript modules. Let’s look at how they work.
655
+
Now that we’ve discussed how NativeScript apps and Angular 2 web apps are similar, let’s move onto how they’re different. When you’re building with NativeScript you have access to the entirety of iOS and Android, as well as the highly capable devices they run on—enabling you to build rich experiences that go above and beyond what the web can offer.
656
+
657
+
The best part is, one NativeScript feature makes it trivial to tie into this native device functionality: NativeScript modules.
Copy file name to clipboardExpand all lines: src/chapters/chapter4.md
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
## NativeScript modules
2
2
3
-
In this chapter you'll learn about NativeScript modules, which are the JavaScript modules in your app's `node_modules/tns-core-modules` folder. Whether you've realized it or not, you've already used several NativeScript modules, as all of the NativeScript UI elements are actually implemented with JavaScript code.
3
+
In this chapter you'll learn about NativeScript modules, which are the TypeScript modules in your app's `node_modules/tns-core-modules` folder. Whether you've realized it or not, you've already used several NativeScript modules, as all of the NativeScript UI elements are actually implemented with TypeScript code.
4
4
5
5
If you dig into `node_modules/tns-core-modules` you can get an idea of how these modules work. Start by finding the `node_modules/tns-core-modules/camera` folder, which includes the implementation of the camera module. It includes:
6
6
@@ -9,7 +9,9 @@ If you dig into `node_modules/tns-core-modules` you can get an idea of how these
9
9
- a file containing the module's iOS implementation (`camera.ios.js`);
10
10
- a file containing code shared by the Android and iOS implementations (`camera-common.js`)
11
11
12
-
> **NOTE**: You can refer to the [Node.js documentation on folders as modules](https://nodejs.org/api/modules.html#modules_folders_as_modules) for more detailed information on how NativeScript organizes its modules.
12
+
> **NOTE**:
13
+
> * You can refer to the [Node.js documentation on folders as modules](https://nodejs.org/api/modules.html#modules_folders_as_modules) for more detailed information on how NativeScript organizes its modules.
14
+
> * The “tns-core-modules” package only includes compiled JavaScript code to cut down on file size. You can find the TypeScript code for each of these modules in the `[main NativeScript GitHub repo](https://github.com/NativeScript/nativescript), for instance here’s the [camera module’s source code](https://github.com/NativeScript/NativeScript/tree/master/camera).
13
15
14
16
The `*.ios.*` and `*.android.*` naming convention should look familiar, as it’s the exact same convention we used to include Android- and iOS-specific styling in [chapter 2.3](#css). NativeScript uses this same convention to implement its modules on iOS and Android. Now that you know where these modules are, let's take a closer look at what else they can do for your app, starting with a closer looks at what you can do with NativeScript’s UI elements.
15
17
@@ -28,6 +30,8 @@ import {topmost} from "ui/frame";
28
30
import {Page} from"ui/page";
29
31
```
30
32
33
+
> **NOTE**: All of the imports you’ve seen to this point work because the TypeScript compiler resolves them against your project’s `node_modules` folder. For instance, `import {Component} from "angular2/core"` works because a `node_modules/angular2/core.d.ts` file exists. The two imports above are NativeScript module imports, and they work because your project’s `references.d.ts` file includes a reference to a TypeScript declaration file (a `.d.ts` file), that lives in `node_modules/tns-core-modules`, and which allows you to import modules from `node_modules/tns-core-modules` without any prefixes.
34
+
31
35
Next, alter the same file’s existing `"angular2/core"` import to include the `OnInit` interface:
32
36
33
37
```TypeScript
@@ -40,7 +44,7 @@ import {Component, OnInit} from "angular2/core";
40
44
exportclassLoginPageimplementsOnInit {
41
45
```
42
46
43
-
If you’re using an editor that supports TypeScript, you should see an error that says something like *“Class ‘LoginPage’ incorrectly implements interface ‘OnInit’”*. When you implement a TypeScript class interface, you’re telling the TypeScript compiler that you will implement any of the methods that the interface requires. In the case of `OnInit`, Angular 2 requires you to implement a single `ngOnInit()` method. To implement it, addthefollowingcodewithinthe`LoginPage` class:
47
+
If you’re using an editor that supports TypeScript, you should see an error that says something like *“Class ‘LoginPage’ incorrectly implements interface ‘OnInit’”*. When you implement a TypeScript class interface, you’re telling the TypeScript compiler that you must implement all methods that the interface requires. In the case of `OnInit`, Angular 2 requires you to implement a single `ngOnInit()` method. To implement it, addthefollowingcodewithinthe`LoginPage` class:
`ngOnInit`isoneofseveral [componentlifecyclehooks](https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html) that Angular 2 provides. As its name implies, `ngOnInit` gets invoked when Angular initializes this component.
0 commit comments