From 852a5850dd83825d5c931379c93c418e53578143 Mon Sep 17 00:00:00 2001 From: Nedyalko Nikolov Date: Mon, 19 Jan 2015 15:17:07 +0200 Subject: [PATCH 1/4] Adding documentation about events. --- bindings.md | 6 +- events.md | 305 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 308 insertions(+), 3 deletions(-) create mode 100644 events.md diff --git a/bindings.md b/bindings.md index 02827603c..55ab2b072 100644 --- a/bindings.md +++ b/bindings.md @@ -19,7 +19,7 @@ Generally almost every UI control (since all controls are created with data bind * Target object should be a successor of **Bindable** class. * Target property should be a **dependency property** in order to use data binding from target to source (or two way data binding). A plain property could be used if there is no need of **twoWay** binding. -* Data (business) object should raise **propertyChanged** event for every change in the value of the property. +* Data (business) object should raise **propertyChange** event for every change in the value of the property. ##Direction of data flow @@ -34,7 +34,7 @@ In order to use this option binding options should set **twoWay as true**. Follo * Creating binding in code. - 1. In order to create a working binding first we should have a source object. Source object should raise **propertyChanged** event for every change of any property. NativeScript has a built-in class that fulfils that requirement (Observable). Following is a code snippet that creates an observable object instance. + 1. In order to create a working binding first we should have a source object. Source object should raise **propertyChange** event for every change of any property. NativeScript has a built-in class that fulfils that requirement (Observable). Following is a code snippet that creates an observable object instance. ``` JavaScript var observableModule = require("data/observable"); @@ -105,7 +105,7 @@ With an xml declaration we set only properies names both for target (text) and s ##Binding source -The important part of the data binding is setting the source object. NativeScript data binding works with any object that emits a **propertyChanged** event. On the process of creating binding source can be set as second parameter of the bind(bindingOptions, source) or could be omitted. In that case for source is used a special property named **bindingContext** of the Bindable class. The special about this property is that it is inheritable across the visual tree. This means that control can use the **bindingContext** (as source) of the first **parent** element with a explicitly set **bindingContext**. With the previous example **bindingContext** can be set either on Page instance or StackPanel instance and TextField will have a proper source for its "text" property binding. +The important part of the data binding is setting the source object. NativeScript data binding works with any object that emits a **propertyChange** event. On the process of creating binding source can be set as second parameter of the bind(bindingOptions, source) or could be omitted. In that case for source is used a special property named **bindingContext** of the Bindable class. The special about this property is that it is inheritable across the visual tree. This means that control can use the **bindingContext** (as source) of the first **parent** element with a explicitly set **bindingContext**. With the previous example **bindingContext** can be set either on Page instance or StackPanel instance and TextField will have a proper source for its "text" property binding. ``` JavaScript page.bindingContext = source; diff --git a/events.md b/events.md new file mode 100644 index 000000000..48f11febd --- /dev/null +++ b/events.md @@ -0,0 +1,305 @@ +--- +nav-title: "NativeScript Events" +title: "Events" +description: "NativeScript Documentation: Events" +position: 66 +--- + +#Events + +##Overview + +Event is a message send from the event emitter for the occurrence of a specific action. This action could be generated by user action (such as finger tap), or by some program logic (indicates that downloading an image from a server is completed). The object that raises event is called **event sender** or just **sender**. The object that consumes the event is called **event listener** or just **listener**. Beeing a TypeScript (JavaScript) framework NativeScript cannot benefits from any language built-in event handling mechanism. Since events are very critical for any modern mobile application development NativeScript provides a class that powers the process of working with events. This class is called Observable ([API-Ref](./ApiReference/data/observable/observable.md)). + +##How events work + +Generally events are very similar to a radio station **sender** plays music (fires an event) and **listener** dance (executes an action). As mentioned NativeScript streamlines the events task with a class called Observable. It is one of the very basic classes within NativeScript framework so almost every NativeScript object (component) has an option for dealing with events. + +##Adding an event listener + +Adding an **event listener** means setting a function (method) which should be executed when event is raised. The example below shows how a function that prints a "Hello World!" message on the console is set to be executed when **button** is tapped. + +* Adding an event handler with a short-hand syntax + +This syntax is used to write an in-line function which should be executed when the **testButton** is tapped. + +``` JavaScript +var buttonModule = require("ui/button"); +var testButton = new buttonModule.Button(); +testButton.text = "Test"; + +testButton.on(buttonModule.knownEvents.tap, function (eventData) { + console.log("Hello World!"); +}); +``` +``` TypeScript +import buttonModule = require("ui/button"); +var testButton = new buttonModule.Button(); +testButton.text = "Test"; + +testButton.on(buttonModule.knownEvents.tap, function (eventData) { + console.log("Hello World!"); + }); +``` + +Even that this shor-hand syntax is very handy to assign some very simple functions it is missing some important features of the original syntax. + +* Adding an event handler + +``` JavaScript +var buttonModule = require("ui/button"); +var testButton = new buttonModule.Button(); +testButton.text = "Test"; + +var onTap = function (eventData) { + console.log("Hello World!"); +}; + +testButton.addEventListener(buttonModule.knownEvents.tap, onTap, this); +``` +``` TypeScript +import buttonModule = require("ui/button"); +var testButton = new buttonModule.Button(); +testButton.text = "Test"; + +var onTap = function (eventData) { + console.log("Hello World!"); +}; + +testButton.addEventListener(buttonModule.knownEvents.tap, onTap, this); +``` +The only difference here is the third optional parameter which represents the **this** argument which will be used as **event handler** context (closure). In other words if you need a **this** argument in your event handler function you have to use the long syntax otherwise **sender** object is used as this. + +* Adding an event handler with an xml declaration + +Another option to set an event handler is via an xml declaration. + +``` XML + + +