|  | 
|  | 1 | +--- | 
|  | 2 | +nav-title: "Getting Started With NativeScript" | 
|  | 3 | +title: "Getting Started With NativeScript" | 
|  | 4 | +description: "NativeScript Documentation: Getting Started" | 
|  | 5 | +position: 12 | 
|  | 6 | +--- | 
|  | 7 | + | 
|  | 8 | +# Getting Started With NativeScript | 
|  | 9 | +NativeScript framework enable developers to use pure JavaScript language to build native mobile applications running on the major mobile platforms - Apple iOS, Google Android. The applications UI stack is built using native UI components and because of that no compromises with the User Experience of the applications are done. | 
|  | 10 | + | 
|  | 11 | +## How It Works | 
|  | 12 | +The native script architectures can be generally explained with this diagram: | 
|  | 13 | + | 
|  | 14 | + | 
|  | 15 | +* **Native OS** - At the bottom level is the native OS (Android, iOS and soon Windows). | 
|  | 16 | +* **NativeScript runtime** runs the JavaScript code of your application. The runtime also provides a way to call all the native APIs of the platform the app is running on. This means that you have access to all the native capabilities of the platform.  | 
|  | 17 | +* **NativeScript Modules** are a set of platform-agnostic libraries that are build on top of the runtime. These modules are wrap the platform specific code, providing a common API. | 
|  | 18 | +* **Application Code** - your application's code. Building an application on top of the NativeScript modules means that you will not have write platform-specific code. This should be the case most of the time. However, you still have the option to reach the native API trough the NativeSctipt runtime. | 
|  | 19 | + | 
|  | 20 | +## Requirements  | 
|  | 21 | +Currently NativeScript can run on the following platforms: | 
|  | 22 | + | 
|  | 23 | +* Android 4.2+ (equivalent to Android API level 17+) | 
|  | 24 | +* iOS 7.1+  | 
|  | 25 | + | 
|  | 26 | +For NativeScript development you have the following options: | 
|  | 27 | + | 
|  | 28 | +* Using the [NativeScript Command-Line Interface](https://github.com/NativeScript/nativescript-cli) | 
|  | 29 | +with a IDE or text editor of your choice. | 
|  | 30 | +* Using [AppBuilder](http://docs.telerik.com/platform/appbuilder/nativescript/index) where you have all the features of [Telerik Platform](http://www.telerik.com/platform) at your disposal. | 
|  | 31 | + | 
|  | 32 | +## Example | 
|  | 33 | +In the following example we will start with a empty NativeScript project and build a simple hello word sample application. | 
|  | 34 | + | 
|  | 35 | +### Creating Blank Project | 
|  | 36 | +Let's start by creating a blank project. As we mentioned you can either use the [NativeScript CLI](https://github.com/NativeScript/nativescript-cli) or NativeScript Blank project template in AppBuilder(available for JavaScript or TypeScript).  | 
|  | 37 | +Form here on we will be working in the `App` folder inside the project.  | 
|  | 38 | + | 
|  | 39 | +### Adding UI | 
|  | 40 | +The project we just created has a single empty page. The UI of the page is defined declaratively in the 'main-page.xml' file. In the project there are also `main-page.js` (or `main-page.ts`) and `main-page.css` files that will hold applications code and styles for this page.  | 
|  | 41 | + | 
|  | 42 | +Let's add some UI in `main-page.xml`: | 
|  | 43 | +```XML | 
|  | 44 | +<?xml version="1.0" encoding="UTF-8" ?> | 
|  | 45 | +<Page> | 
|  | 46 | + <StackPanel> | 
|  | 47 | + <Label text="Tap the button" style="horizontal-align: center"/> | 
|  | 48 | + <Button text="TAP" /> | 
|  | 49 | + <Label text="message" textWrap="true" style="horizontal-align: center"/> | 
|  | 50 | + </StackPanel> | 
|  | 51 | +</Page> | 
|  | 52 | +``` | 
|  | 53 | +We have added a title label, a button and a message label that we are going to use in the next section.  | 
|  | 54 | +Here is the result: | 
|  | 55 | + | 
|  | 56 | + | 
|  | 57 | +*Note: UI declaration is covered in depth in the [UI with XML](ui-with-xml.md) article.* | 
|  | 58 | + | 
|  | 59 | +### Creating a View-Model | 
|  | 60 | +[MVVM](http://en.wikipedia.org/wiki/Model_View_ViewModel) pattern is the preferred approach when developing mobile applications with NativeScript. In this section we will create and bind a view-model to the page we already have.  | 
|  | 61 | +The view-model will hold simple counter which will be used to update a message each time the user taps on the button.  | 
|  | 62 | + | 
|  | 63 | +Create a `view-models` folder and `main-view-model.js` ( or `main-view-model.ts` if you are using TypeScript) file in it: | 
|  | 64 | +``` JavaScript | 
|  | 65 | +var observable = require("data/observable"); | 
|  | 66 | + | 
|  | 67 | +var counter = 42; | 
|  | 68 | +var mainViewModel = new observable.Observable(); | 
|  | 69 | +mainViewModel.set("message", counter + " taps left"); | 
|  | 70 | +mainViewModel.tapAction = function () { | 
|  | 71 | + counter--; | 
|  | 72 | + if (counter <= 0) { | 
|  | 73 | + mainViewModel.set("message", "Hoorraaay! You unlocked the NativeScript clicker achievement!"); | 
|  | 74 | + } | 
|  | 75 | + else { | 
|  | 76 | + mainViewModel.set("message", counter + " taps left"); | 
|  | 77 | + } | 
|  | 78 | +}; | 
|  | 79 | +exports.mainViewModel = mainViewModel; | 
|  | 80 | +``` | 
|  | 81 | +``` TypeScript | 
|  | 82 | +import observable = require("data/observable"); | 
|  | 83 | + | 
|  | 84 | +export class HelloWorldModel extends observable.Observable { | 
|  | 85 | + private counter: number; | 
|  | 86 | + constructor() { | 
|  | 87 | + super(); | 
|  | 88 | + | 
|  | 89 | + this.counter = 42; | 
|  | 90 | + this.set("message", this.counter + " taps left"); | 
|  | 91 | + } | 
|  | 92 | + | 
|  | 93 | + public tapAction() { | 
|  | 94 | + this.counter--; | 
|  | 95 | + if (this.counter <= 0) { | 
|  | 96 | + this.set("message", "Hoorraaay! You unlocked the NativeScript clicker achievement!"); | 
|  | 97 | + } | 
|  | 98 | + else { | 
|  | 99 | + this.set("message", this.counter + " taps left") | 
|  | 100 | + } | 
|  | 101 | + } | 
|  | 102 | +} | 
|  | 103 | +export var mainViewModel = new HelloWorldModel(); | 
|  | 104 | +``` | 
|  | 105 | + | 
|  | 106 | +The view-model is an instance of `Observable` type so that the UI can receive notification whenever the `message` property is set. | 
|  | 107 | + | 
|  | 108 | +Now that we have the main-view-model we will set it as a `bindingContext` of the main-page. We will do this by handling the `pageLoaded` event: | 
|  | 109 | + | 
|  | 110 | +main-page.xml: | 
|  | 111 | +```XML | 
|  | 112 | +<Page loaded="pageLoaded"/> | 
|  | 113 | + ... | 
|  | 114 | +</Page> | 
|  | 115 | +``` | 
|  | 116 | + | 
|  | 117 | +main-page.js (or main-page.ts): | 
|  | 118 | +```JavaScript | 
|  | 119 | +var vmModule = require("./view-models/main-view-model"); | 
|  | 120 | +function pageLoaded(args) { | 
|  | 121 | + var page = args.object; | 
|  | 122 | + page.bindingContext = vmModule.mainViewModel; | 
|  | 123 | +} | 
|  | 124 | +exports.pageLoaded = pageLoaded; | 
|  | 125 | + | 
|  | 126 | +``` | 
|  | 127 | +```TypeScript | 
|  | 128 | +import observable = require("data/observable"); | 
|  | 129 | +import pages = require("ui/page"); | 
|  | 130 | +import vmModule = require("./view-models/main-view-model"); | 
|  | 131 | + | 
|  | 132 | +// Event handler for Page "loaded" event attached in main-page.xml | 
|  | 133 | +export function pageLoaded(args: observable.EventData) { | 
|  | 134 | + // Get the event sender | 
|  | 135 | + var page = <pages.Page>args.object; | 
|  | 136 | + page.bindingContext = vmModule.mainViewModel; | 
|  | 137 | +} | 
|  | 138 | +``` | 
|  | 139 | + | 
|  | 140 | +The last thing we need to do is to bind the UI elements in the XML to the view-model: | 
|  | 141 | +main-page.xml | 
|  | 142 | +``` XML | 
|  | 143 | +<?xml version="1.0" encoding="UTF-8" ?> | 
|  | 144 | +<Page loaded="pageLoaded"> | 
|  | 145 | + <StackPanel> | 
|  | 146 | + <Label text="Tap the button" style="horizontal-align: center"/> | 
|  | 147 | + <Button text="TAP" tap="{{ tapAction }}"/> | 
|  | 148 | + <Label text="{{ message }}" textWrap="true" style="horizontal-align: center"/> | 
|  | 149 | + </StackPanel> | 
|  | 150 | +</Page> | 
|  | 151 | +``` | 
|  | 152 | + | 
|  | 153 | +Here is the result: | 
|  | 154 | + | 
|  | 155 | + | 
|  | 156 | +We used data-binding for the `tap` event of the button and the `text` of the message-label. | 
|  | 157 | +*Note: Binding is covered in depth in the [Data Binding](bindings.md) article.* | 
|  | 158 | + | 
|  | 159 | +### Adding Styles | 
|  | 160 | +The final step in this tutorial will be to style the application. Styling in NativeScript is done with a subset of the CSS syntax. | 
|  | 161 | +For each page, the NativeScript runtime will automatically load and apply the CSS file that has the same name as the XML file for the page. | 
|  | 162 | + | 
|  | 163 | +We will add the CSS in `main-page.css`: | 
|  | 164 | +```CSS | 
|  | 165 | +.title { | 
|  | 166 | + font-size: 30; | 
|  | 167 | + horizontal-align: center; | 
|  | 168 | + margin:20; | 
|  | 169 | +} | 
|  | 170 | + | 
|  | 171 | +button { | 
|  | 172 | + font-size: 42; | 
|  | 173 | + horizontal-align: center; | 
|  | 174 | +} | 
|  | 175 | + | 
|  | 176 | +.message { | 
|  | 177 | + font-size: 20; | 
|  | 178 | + color: #284848; | 
|  | 179 | + margin:10 40; | 
|  | 180 | + horizontal-align: center; | 
|  | 181 | +} | 
|  | 182 | +``` | 
|  | 183 | + | 
|  | 184 | +Finally - replace the inline styles in the `main-page.xml` with `cssClass` attribute, so that the CSS classes we defined are applied: | 
|  | 185 | +```XML | 
|  | 186 | +<?xml version="1.0" encoding="UTF-8" ?> | 
|  | 187 | +<Page loaded="pageLoaded"> | 
|  | 188 | + <StackPanel> | 
|  | 189 | + <Label text="Tap the button" cssClass="title"/> | 
|  | 190 | + <Button text="TAP" tap="{{ tapAction }}" /> | 
|  | 191 | + <Label text="{{ message }}" cssClass="message" textWrap="true"/> | 
|  | 192 | + </StackPanel> | 
|  | 193 | +</Page> | 
|  | 194 | +``` | 
|  | 195 | + | 
|  | 196 | +Here is the result: | 
|  | 197 | + | 
|  | 198 | + | 
|  | 199 | +*Note: CSS Styling is covered in depth in the [Styling](styling.md) article.* | 
|  | 200 | + | 
|  | 201 | +## Next Steps | 
|  | 202 | +Read the advanced topics below or refer to the [Api Reference](ApiReference/) to continue wit NativeScript development: | 
|  | 203 | + | 
|  | 204 | +* [Application](application-management.md) | 
|  | 205 | +* [Navigation](navigation.md) | 
|  | 206 | +* [Layouts](layouts.md) | 
|  | 207 | +* [Styling](styling.md) | 
|  | 208 | +* [Binding](bindings.md) | 
|  | 209 | +* [UI with XML](ui-with-xml.md) | 
|  | 210 | +* [UI Views](ui-views.md) | 
|  | 211 | +* [UI Dialogs](ui-dialogs.md) | 
|  | 212 | +* [Location](location.md) | 
|  | 213 | +* [Modules](modules.md) | 
0 commit comments