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
Copy file name to clipboardExpand all lines: index.html
+63Lines changed: 63 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -559,6 +559,69 @@ <h3 id="structuring-your-app">Structuring your app</h3>
559
559
<p>There are many reasons to segment your application into modular units, and you can <ahref="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>
560
560
<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>
561
561
<p>To see how this works in action let’s create a few files.</p>
562
+
<h4class="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><codeclass="lang-JavaScript">export class User {
568
+
email: string;
569
+
password: string;
570
+
}
571
+
</code></pre>
572
+
<p>This code defines a simple <ahref="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 <ahref="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><codeclass="lang-JavaScript">import {User} from "./shared/user/user";
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><codeclass="lang-JavaScript">export class AppComponent {
<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><TextField></code>s with the code shown below, which updates the <code>[(ngModel)]</code> bindings to point at the new <code>User</code> object:</p>
<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><codeclass="lang-JavaScript">import {Component} from "angular2/core";
596
+
import {User} from "./shared/user/user";
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+79Lines changed: 79 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,7 +116,86 @@ Even if you have no plans to create an Angular 2 web app, separating out your co
116
116
117
117
To see how this works in action let’s create a few files.
118
118
119
+
<h4class="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
+
exportclassUser {
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
+
exportclassAppComponent {
146
+
user: User;
147
+
constructor() {
148
+
this.user=newUser();
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:
0 commit comments