Observable
The Observable
class is at the foundation of data binding in Nativescript.
Data binding is the process of connecting application user interface (UI) to a data object (code).
It enables constant change propagation by reflecting UI modifications in the code and vice versa.
In order for the changes propagation to occur, the property in the code has to raise a propertyChange
event to notify the UI component about the changes.
The Observable
class is what raises the propertyChange
events.
Read more on data binding in this article.
Usage
Use the set()
method to add the different properties and event handlers you want to bind the UI to.
Creating Observable with fromObject() Method
The fromObject()
method creates an Observable
instance and sets its properties according to the supplied JavaScript object.
import { fromObject } from '@nativescript/core'
const newViewModel = fromObject({ myColor: 'Lightgray' })
// the above is equal to
/*
let newViewModel = new Observable();
newViewModel.set("myColor", "Lightgray");
*/
const newViewModel: Observable = fromObject({ myColor: 'Lightgray' })
// the above is equal to
/*
let newViewModel = new Observable();
newViewModel.set("myColor", "Lightgray");
*/
Creating Observable with fromObjectRecursive() Method
The fromObjectRecursive()
method creates an Observable instance and sets its properties according to the supplied JavaScript object.
This function will create new Observables for each nested object (except arrays and functions) from a supplied JavaScript object.
// fromObjectRecursive will create new Observable for each nested object (except arrays and functions)
const nestedViewModel = fromObjectRecursive({
client: 'John Doe',
favoriteColor: { hisColor: 'Green' } // favoriteColor is an Observable (using recursive creation of Observables)
})
// the above is equal to
/*
let newViewModel2 = new Observable();
newViewModel2.set("client", "John Doe");
newViewModel2.set("favoriteColor", fromObject( {hisColor: "Green" }));
*/
// fromObjectRecursive will create new Observable for each nested object (except arrays and functions)
const nestedViewModel: Observable = fromObjectRecursive({
client: 'John Doe',
favoriteColor: { hisColor: 'Green' } // favoriteColor is an Observable (using recursive creation of Observables)
})
// the above is equal to
/*
let newViewModel2 = new Observable();
newViewModel2.set("client", "John Doe");
newViewModel2.set("favoriteColor", fromObject( {hisColor: "Green" }));
*/
Adding Event Listener and Using propertyChangeEvent
Using propertyChangeEvent and responding to property changes with arguments of type PropertyChangeData.
const myListener = viewModel.addEventListener(Observable.propertyChangeEvent, args => {
// args is of type PropertyChangeData
console.log('propertyChangeEvent [eventName]: ', args.eventName)
console.log('propertyChangeEvent [propertyName]: ', args.propertyName)
console.log('propertyChangeEvent [value]: ', args.value)
console.log('propertyChangeEvent [oldValue]: ', args.oldValue)
})
const myListener = viewModel.addEventListener(Observable.propertyChangeEvent, args => {
// args is of type PropertyChangeData
console.log('propertyChangeEvent [eventName]: ', args.eventName)
console.log('propertyChangeEvent [propertyName]: ', args.propertyName)
console.log('propertyChangeEvent [value]: ', args.value)
console.log('propertyChangeEvent [oldValue]: ', args.oldValue)
})
Static properties
Name | Type | Description |
---|---|---|
|
|
String value used when hooking to propertyChange event. |
Static methods
Name | Return Type | Description |
---|---|---|
|
|
A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
+ + Parameters: + |
|
|
The name(s) of the event(s) to attach to.
+ + Parameters: + |
|
|
Shortcut alias to the removeEventListener method. |
|
|
Adds a listener for the specified event name. |
|
|
The name(s) of the event(s) to attach to.
+ + Parameters: + |
Instance methods
All the static methods above have their Observable
instance counterparts.
Name | Return Type | Description |
---|---|---|
|
|
Creates or updates the specified property with the provided value. |
|
|
Updates the specified property with the provided value and raises a property change event and a specific change event based on the property name. |
|
|
Gets the value of the specified property. |
|
|
Notifies all the registered listeners for the event provided in the data.eventName.
+ + Parameter(s): + |
|
|
Notifies all the registered listeners for the property change event. |
|
|
Checks whether a listener is registered for the specified event name.
+ |
propertyChangeEvent data
Name | Type | Description |
---|---|---|
|
|
The name of the event. |
|
|
The Observable instance that has raised the event. |
|
|
The name of the property that has changed. |
|
|
The new value of the property. |
|
|
The previous value of the property. |
Other functions
Name | Return Type | Description |
---|---|---|
|
|
Creates an |
|
|
Creates an Observable instance and sets its properties according to the supplied JavaScript object.
This function will create new Observable for each nested object (except arrays and functions) from a supplied JavaScript object.
+ |