Skip to content

Commit 48a57bd

Browse files
author
Nedyalko Nikolov
committed
Creating observables from a nested json.
1 parent d398678 commit 48a57bd

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

tests/app/ui/bindable-tests.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,4 +1261,46 @@ export function test_only_Bindable_BindingContext_Null_DoesNotThrow() {
12611261
obj.bind(options);
12621262
obj.bindingContext = new observable.Observable({ a: "b" });
12631263
obj.bindingContext = null;
1264+
}
1265+
1266+
export function test_Observable_from_nested_json_binds_correctly() {
1267+
let expectedValue = "Test";
1268+
var model = new observable.Observable({
1269+
"firstObject": {
1270+
"secondObject": {
1271+
"dummyProperty": "text"
1272+
}
1273+
}
1274+
});
1275+
1276+
var obj = new bindable.Bindable();
1277+
obj.bind({
1278+
sourceProperty: "firstObject.secondObject.dummyProperty",
1279+
targetProperty: "test"
1280+
}, model);
1281+
1282+
model.get("firstObject").get("secondObject").set("dummyProperty", expectedValue);
1283+
1284+
TKUnit.assertEqual(obj.get("test"), expectedValue);
1285+
}
1286+
1287+
export function test_Observable_from_nested_json_binds_correctly_when_upper_object_is_changed() {
1288+
let expectedValue = "Test";
1289+
var model = new observable.Observable({
1290+
"firstObject": {
1291+
"secondObject": {
1292+
"dummyProperty": "text"
1293+
}
1294+
}
1295+
});
1296+
1297+
var obj = new bindable.Bindable();
1298+
obj.bind({
1299+
sourceProperty: "firstObject.secondObject.dummyProperty",
1300+
targetProperty: "test"
1301+
}, model);
1302+
1303+
model.get("firstObject").set("secondObject", new observable.Observable({"dummyProperty": expectedValue}));
1304+
1305+
TKUnit.assertEqual(obj.get("test"), expectedValue);
12641306
}

tns-core-modules/data/observable/observable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export class Observable implements definition.Observable {
5656
this._map = new Map<string, Object>();
5757
for (var prop in json) {
5858
if (json.hasOwnProperty(prop)) {
59+
if (!Array.isArray(json[prop]) && typeof json[prop] === 'object') {
60+
json[prop] = new Observable(json[prop]);
61+
}
5962
this._defineNewProperty(prop);
6063
this.set(prop, json[prop]);
6164
}

0 commit comments

Comments
 (0)