Observable

Using Observable objects requires the "data/observable" module.

import { Observable, fromObject, fromObjectRecursive, PropertyChangeData, EventData, WrappedValue } from "tns-core-modules/data/observable";

Creating an Observable

var json = {
    Name: "John",
    Age: 34,
    Married: true
};
var person = fromObject(json);
var name = person.get("Name");
var age = person.get("Age");
var married = person.get("Married");
// console.log(name + " " + age + " " + married); // Prints out "John 34 true" if uncommented.

Responding to property changes

var person = new Observable();
person.set("Name", "John");
person.set("Age", 34);
person.set("Married", true);
person.addEventListener(Observable.propertyChangeEvent, function (pcd: PropertyChangeData) {
    //console.log(pcd.eventName.toString() + " " + pcd.propertyName.toString() + " " + pcd.value.toString());
});
person.set("Age", 35);
person.set("Married", false);
// If uncommented, the console.log above produces the following output:
// propertyChange Age 35
// propertyChange Married false

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript
NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.