Merge pull request #218 from NativeScript/observable-json

Implemented Observable constructor that accepts JSON objects.
This commit is contained in:
Rossen Hristov
2015-03-24 12:30:18 +02:00
3 changed files with 37 additions and 0 deletions

View File

@@ -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 () {
// <snippet module="data/observable" title="data/observable">
// ### Responding to property changes

View File

@@ -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.
*/
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".
*/

View File

@@ -13,6 +13,16 @@ export module knownEvents {
export class Observable implements definition.Observable {
private _observers = {};
constructor(json?: any) {
if (json) {
for (var prop in json) {
if (json.hasOwnProperty(prop)) {
this.set(prop, json[prop]);
}
}
}
}
get typeName(): string {
return types.getClass(this);
}