mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #218 from NativeScript/observable-json
Implemented Observable constructor that accepts JSON objects.
This commit is contained in:
@@ -18,6 +18,27 @@ class TestObservable extends observable.Observable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export var test_Observable_Constructor = function () {
|
||||||
|
// <snippet module="data/observable" title="data/observable">
|
||||||
|
// ### Creating an Observable
|
||||||
|
// ``` JavaScript
|
||||||
|
var json = {
|
||||||
|
Name: "John",
|
||||||
|
Age: 34,
|
||||||
|
Married: true
|
||||||
|
};
|
||||||
|
var person = new observable.Observable(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.
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
TKUnit.assert(name === "John", "Expected name is John");
|
||||||
|
TKUnit.assert(age === 34, "Expected age is 34");
|
||||||
|
TKUnit.assert(married === true, "Expected married is true");
|
||||||
|
}
|
||||||
|
|
||||||
export var tests_DummyTestForCodeSnippet = function () {
|
export var tests_DummyTestForCodeSnippet = function () {
|
||||||
// <snippet module="data/observable" title="data/observable">
|
// <snippet module="data/observable" title="data/observable">
|
||||||
// ### Responding to property changes
|
// ### Responding to property changes
|
||||||
|
|||||||
6
data/observable/observable.d.ts
vendored
6
data/observable/observable.d.ts
vendored
@@ -44,6 +44,12 @@ declare module "data/observable" {
|
|||||||
* Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.
|
* Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.
|
||||||
*/
|
*/
|
||||||
class Observable {
|
class Observable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an Observable instance and sets its properties accroding to the supplied JSON object.
|
||||||
|
*/
|
||||||
|
constructor(json?: any);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the name of the constructor function for this instance. E.g. for a Button class this will return "Button".
|
* Gets the name of the constructor function for this instance. E.g. for a Button class this will return "Button".
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -13,6 +13,16 @@ export module knownEvents {
|
|||||||
export class Observable implements definition.Observable {
|
export class Observable implements definition.Observable {
|
||||||
private _observers = {};
|
private _observers = {};
|
||||||
|
|
||||||
|
constructor(json?: any) {
|
||||||
|
if (json) {
|
||||||
|
for (var prop in json) {
|
||||||
|
if (json.hasOwnProperty(prop)) {
|
||||||
|
this.set(prop, json[prop]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get typeName(): string {
|
get typeName(): string {
|
||||||
return types.getClass(this);
|
return types.getClass(this);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user