Merge pull request #2574 from NativeScript/nnikolov/NestedObservablesFix

Fixed creating Observable object from nested JSON object.
This commit is contained in:
Nedyalko Nikolov
2016-08-12 18:13:54 +03:00
committed by GitHub
4 changed files with 52 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import dependencyObservable = require("ui/core/dependency-observable");
import TKUnit = require("../TKUnit"); import TKUnit = require("../TKUnit");
import types = require("utils/types"); import types = require("utils/types");
import proxy = require("ui/core/proxy"); import proxy = require("ui/core/proxy");
import {ObservableArray} from "data/observable-array";
var TESTED_NAME = "tested"; var TESTED_NAME = "tested";
class TestObservable extends observable.Observable { class TestObservable extends observable.Observable {
@ -525,3 +526,12 @@ export function test_CorrectPropertyValueAfterUsingWrappedValue() {
TKUnit.assertEqual(testObservable.get("property1"), testArray, "WrappedValue is used only to execute property change logic and unwrapped value should be used as proeprty value."); TKUnit.assertEqual(testObservable.get("property1"), testArray, "WrappedValue is used only to execute property change logic and unwrapped value should be used as proeprty value.");
} }
export function test_NestedObservablesWithObservableArrayShouldNotCrash() {
let someObservableArray = new ObservableArray<any>();
let testObservable = observable.Observable.fromJSONRecursive({
firstProp: "test string",
secondProp: someObservableArray
});
TKUnit.assert(testObservable !== undefined);
}

View File

@ -1265,7 +1265,7 @@ export function test_only_Bindable_BindingContext_Null_DoesNotThrow() {
export function test_Observable_from_nested_json_binds_correctly() { export function test_Observable_from_nested_json_binds_correctly() {
let expectedValue = "Test"; let expectedValue = "Test";
var model = new observable.Observable({ let model = observable.Observable.fromJSONRecursive({
"firstObject": { "firstObject": {
"secondObject": { "secondObject": {
"dummyProperty": "text" "dummyProperty": "text"
@ -1286,7 +1286,7 @@ export function test_Observable_from_nested_json_binds_correctly() {
export function test_Observable_from_nested_json_binds_correctly_when_upper_object_is_changed() { export function test_Observable_from_nested_json_binds_correctly_when_upper_object_is_changed() {
let expectedValue = "Test"; let expectedValue = "Test";
var model = new observable.Observable({ let model = observable.Observable.fromJSONRecursive({
"firstObject": { "firstObject": {
"secondObject": { "secondObject": {
"dummyProperty": "text" "dummyProperty": "text"

View File

@ -70,8 +70,19 @@ declare module "data/observable" {
*/ */
public static propertyChangeEvent: string; public static propertyChangeEvent: string;
/**
* Creates an Observable instance and sets its properties according to the supplied JSON object.
*/
public static fromJSON(json: any): Observable;
/** /**
* Creates an Observable instance and sets its properties according to the supplied JSON object. * Creates an Observable instance and sets its properties according to the supplied JSON object.
* This function will create new Observable for each nested object (expect arrays and functions) from supplied JSON.
*/
public static fromJSONRecursive(json: any): Observable;
/**
* [Deprecated please use static functions fromJSON or fromJSONRecursive instead] Creates an Observable instance and sets its properties according to the supplied JSON object.
*/ */
constructor(json?: any); constructor(json?: any);

View File

@ -51,18 +51,37 @@ export class Observable implements definition.Observable {
private _observers = {}; private _observers = {};
public static fromJSON(json: any): Observable {
let observable = new Observable();
observable.addPropertiesFromJSON(observable, json, false);
return observable;
}
public static fromJSONRecursive(json: any): Observable {
let observable = new Observable();
observable.addPropertiesFromJSON(observable, json, true);
return observable;
}
private addPropertiesFromJSON(observable: Observable, json: any, recursive?: boolean) {
let isRecursive = recursive || false;
observable._map = new Map<string, Object>();
for (var prop in json) {
if (json.hasOwnProperty(prop)) {
if (isRecursive) {
if (!Array.isArray(json[prop]) && typeof json[prop] === 'object' && types.getClass(json[prop]) !== 'ObservableArray') {
json[prop] = Observable.fromJSONRecursive(json[prop]);
}
}
observable._defineNewProperty(prop);
observable.set(prop, json[prop]);
}
}
}
constructor(json?: any) { constructor(json?: any) {
if (json) { if (json) {
this._map = new Map<string, Object>(); this.addPropertiesFromJSON(this, json);
for (var prop in json) {
if (json.hasOwnProperty(prop)) {
if (!Array.isArray(json[prop]) && typeof json[prop] === 'object') {
json[prop] = new Observable(json[prop]);
}
this._defineNewProperty(prop);
this.set(prop, json[prop]);
}
}
} }
} }