Creating observables from a nested json.

This commit is contained in:
Nedyalko Nikolov
2016-07-18 17:34:17 +03:00
parent d3986780ca
commit 48a57bdaf3
2 changed files with 45 additions and 0 deletions

View File

@ -1261,4 +1261,46 @@ export function test_only_Bindable_BindingContext_Null_DoesNotThrow() {
obj.bind(options);
obj.bindingContext = new observable.Observable({ a: "b" });
obj.bindingContext = null;
}
export function test_Observable_from_nested_json_binds_correctly() {
let expectedValue = "Test";
var model = new observable.Observable({
"firstObject": {
"secondObject": {
"dummyProperty": "text"
}
}
});
var obj = new bindable.Bindable();
obj.bind({
sourceProperty: "firstObject.secondObject.dummyProperty",
targetProperty: "test"
}, model);
model.get("firstObject").get("secondObject").set("dummyProperty", expectedValue);
TKUnit.assertEqual(obj.get("test"), expectedValue);
}
export function test_Observable_from_nested_json_binds_correctly_when_upper_object_is_changed() {
let expectedValue = "Test";
var model = new observable.Observable({
"firstObject": {
"secondObject": {
"dummyProperty": "text"
}
}
});
var obj = new bindable.Bindable();
obj.bind({
sourceProperty: "firstObject.secondObject.dummyProperty",
targetProperty: "test"
}, model);
model.get("firstObject").set("secondObject", new observable.Observable({"dummyProperty": expectedValue}));
TKUnit.assertEqual(obj.get("test"), expectedValue);
}

View File

@ -56,6 +56,9 @@ export class Observable implements definition.Observable {
this._map = new Map<string, Object>();
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]);
}