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
+10-7Lines changed: 10 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -611,14 +611,14 @@ <h4 class="exercise-start">
611
611
<p>At this point, you have a basic login screen setup with two-way data binding—not bad for 20 some lines of code of TypeScript. (Think about how much code you’d have to write in Android Studio <em>and</em> Xcode to accomplish the same task.) To this point though you’ve been placing all of your logic in a single TypeScript file, which doesn’t scale all that well for real-world applications.</p>
612
612
<p>Before we tie this app to a backend and make this login screen fully functional, let’s take a step back and setup a structure that can scale.</p>
613
613
<h3id="structuring-your-app">Structuring your app</h3>
614
-
<p>There are many reasons to segment any 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>
615
-
<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>
616
-
<p>To see how this works in action let’s edit some files in the <code>/shared</code> folder and set them up to be imported.</p>
614
+
<p>There are many reasons to segment any 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-built native apps.</p>
615
+
<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 in your native and web apps can be an invaluable time saver.</p>
616
+
<p>To see how this works in action, let’s edit some files in the <code>/shared</code> folder and set them up to be imported.</p>
617
617
<h4class="exercise-start">
618
618
<b>Exercise</b>: Add a model object
619
619
</h4>
620
620
621
-
<p>The first thing we’ll do is add a model object to store user data. Open <code>app/user/user.ts</code> and paste in the following code:</p>
621
+
<p>Let’s start by creating a simple model object to store user data. Open <code>app/user/user.ts</code> and paste in the following code:</p>
622
622
<pre><codeclass="lang-JavaScript">export class User {
623
623
email: string;
624
624
password: string;
@@ -628,7 +628,7 @@ <h4 class="exercise-start">
628
628
<p>Next, open <code>app/app.component.ts</code>, and first add the following <code>import</code> to the top of the file:</p>
629
629
<pre><codeclass="lang-JavaScript">import {User} from "./shared/user/user";
630
630
</code></pre>
631
-
<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>
631
+
<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 here. 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>
632
632
<p>Next, replace the existing <code>AppComponent</code> definition with the one below, which uses the <code>User</code> class you just imported.</p>
633
633
<pre><codeclass="lang-JavaScript">export class AppComponent {
634
634
user: User;
@@ -640,8 +640,11 @@ <h4 class="exercise-start">
640
640
}
641
641
}
642
642
</code></pre>
643
-
<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>
644
-
<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>
643
+
<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 instantiate a new instance of the <code>User</code> class in a new <code>constructor</code> function, that Angular 2 will invoke when it bootstraps your application.</p>
644
+
<blockquote>
645
+
<p><strong>NOTE</strong>: Remember that in <code>main.ts</code> you’re calling <code>nativeScriptBootstrap(AppComponent)</code>. Under the hood, Angular 2 will instantiate an instance of <code>AppComponent</code> during the bootstrapping process, which will cause your <code>AppComponent</code> constructor function to run.</p>
646
+
</blockquote>
647
+
<p>Your final step is to use this new model object in your template. To do that, replace the two existing <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>
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,17 +110,17 @@ Before we tie this app to a backend and make this login screen fully functional,
110
110
111
111
### Structuring your app
112
112
113
-
There are many reasons to segment any application into modular units, and you can [read about the various benefits on Wikipedia](https://en.wikipedia.org/wiki/Modular_programming). 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.
113
+
There are many reasons to segment any application into modular units, and you can [read about the various benefits on Wikipedia](https://en.wikipedia.org/wiki/Modular_programming). 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-built native apps.
114
114
115
-
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 _do_ 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.
115
+
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 _do_ have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse in your native and web apps can be an invaluable time saver.
116
116
117
-
To see how this works in action let’s edit some files in the `/shared` folder and set them up to be imported.
117
+
To see how this works in action, let’s edit some files in the `/shared` folder and set them up to be imported.
118
118
119
119
<h4class="exercise-start">
120
120
<b>Exercise</b>: Add a model object
121
121
</h4>
122
122
123
-
The first thing we’ll do is add a model object to store user data. Open `app/user/user.ts` and paste in the following code:
123
+
Let’s start by creating a simple model object to store user data. Open `app/user/user.ts` and paste in the following code:
124
124
125
125
```JavaScript
126
126
exportclassUser {
@@ -137,7 +137,7 @@ Next, open `app/app.component.ts`, and first add the following `import` to the t
137
137
import {User} from"./shared/user/user";
138
138
```
139
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.
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 here. 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
141
142
142
Next, replace the existing `AppComponent` definition with the one below, which uses the `User` class you just imported.
143
143
@@ -153,9 +153,11 @@ export class AppComponent {
153
153
}
154
154
```
155
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`.
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 instantiate a new instance of the `User` class in a new `constructor` function, that Angular 2 will invoke when it bootstraps your application.
157
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:
158
+
> **NOTE**: Remember that in `main.ts` you’re calling `nativeScriptBootstrap(AppComponent)`. Under the hood, Angular 2 will instantiate an instance of `AppComponent` during the bootstrapping process, which will cause your `AppComponent` constructor function to run.
159
+
160
+
Your final step is to use this new model object in your template. To do that, replace the two existing `<TextField>`s with the code shown below, which updates the `[(ngModel)]` bindings to point at the new `User` object:
0 commit comments