Skip to content

Commit e23e490

Browse files
committed
Only pre-pop fields that exist in the result #219
1 parent 5b43fe8 commit e23e490

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

grails-app/assets/javascripts/forms-knockout-bindings.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,16 +1207,23 @@
12071207
if (!target) {
12081208
throw "Unable to locate target for pre-population: "+target;
12091209
}
1210-
if (_.isFunction(target.loadData)) {
1211-
target.loadData(data);
1212-
} else if (_.isFunction(target.load)) {
1213-
target.load(data);
1214-
} else if (ko.isObservable(target)) {
1215-
target(data);
1216-
} else {
1217-
console.log("Warning: target for pre-populate is invalid");
1210+
target = target.data || target;
1211+
for (var prop in data) {
1212+
if (target.hasOwnProperty(prop)) {
1213+
var propTarget = target[prop];
1214+
if (_.isFunction(propTarget.loadData)) {
1215+
propTarget.loadData(data[prop]);
1216+
} else if (_.isFunction(propTarget.load)) {
1217+
propTarget.load(data[prop]);
1218+
} else if (ko.isObservable(propTarget)) {
1219+
propTarget(data[prop]);
1220+
} else {
1221+
console.log("Warning: target for pre-populate is invalid");
1222+
}
1223+
}
12181224
}
12191225

1226+
12201227
}); // This is a computed rather than a pureComputed as it has a side effect.
12211228
});
12221229
}

grails-app/conf/example_models/behavioursExample.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,13 @@
4545
"name": "param",
4646
"type": "computed",
4747
"expression": "item5"
48-
},
49-
{
50-
"name": "item5",
51-
"type": "computed",
52-
"expression": "item5"
5348
}
5449
]
5550
},
5651
"mapping": [
5752
{
5853
"source-path": "param",
5954
"target": "item6"
60-
},
61-
{
62-
"source-path": "item5",
63-
"target": "item5"
6455
}
6556
],
6657
"target": "$data"
@@ -119,7 +110,7 @@
119110
"items": [
120111
{
121112
"type": "literal",
122-
"source": "Note for this example, data entered into item5 will trigger a pre-pop call and be mapped back to item5 and item6. Note that the target of the pre-pop is $data which is the current binding context (or the root object in this case). A current limitation is the load method is used, which means if the pre-pop result does not contain keys for all data in the target object, the data for missing fields will be set to undefined. A planned enhancement is to only replace data where keys in the pre-pop data exist."
113+
"source": "Note for this example, data entered into item5 will trigger a pre-pop call and be mapped back to item6. Note that the target of the pre-pop is $data which is the current binding context (or the root object in this case). A current limitation is the load method is used for all keys in the returned data, which means if the data is nested and the pre-pop result does not contain keys for all nested data in the nested target object, the data for missing fields will be set to undefined."
123114
}
124115
]
125116
}

0 commit comments

Comments
 (0)