concept event in category nativescript

appears as: events, n event, An event, event, event
NativeScript in Action

This is an excerpt from Manning's book NativeScript in Action.

After creating your user interface (UI) using XML, you use CSS to style the UI (like CSS is used to style HTML apps). Then, you write JavaScript to augment your UI. Your JavaScript code will contain writing business logic that responds to events (like the app startup event) and interactions (like a button tap or finger swipe). These three pieces (UI written with XML, CSS, and business logic written with JavaScript) combine to create your app code.

Lifecycle hooks are a way of tapping into the key life moments of Angular components, allowing you the execute code when an event happens. There are a variety of lifecycle hooks to tap into. One event, ngOnInit is called when a component is initialized and displays data-bound properties. If you’re using services to get data to display in the UI, retrieving that data during the ngOnInit lifecycle hook is the right time to do it. To learn about other lifecycle hooks, check out https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html.

Handling events with event data

Handling events bound with the $event keyword is just like handling simple data-bound events. Let’s start by adding the event handler code from listing 17.11 to the ListComponent class.

Listing 17.11 The list.component.ts file updated to include the itemTap event handler
import { ItemEventData } from "ui/list-view"; onItemTap(args: ItemEventData): void { #1 let id = args.index; this.routerExtensions.navigate(["detail", id]); } #1 the $event keyword injects a parameter into the event handler

When the $event keyword is used in databinding an event, it injects event data into the event handler as a parameter. The datatype of the injected parameter varies depending on the event type. There’s not enough room to outline every UI event here, and you don’t need to memorize them because it’s easy to look up online. Check out the official NativeScript API at https://docs.nativescript.org/api-reference/globals.html for more information.