Skip to content

Commit

Permalink
Only pre-pop fields that exist in the result #219
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Jan 4, 2024
1 parent 5b43fe8 commit e23e490
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
23 changes: 15 additions & 8 deletions grails-app/assets/javascripts/forms-knockout-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,16 +1207,23 @@
if (!target) {
throw "Unable to locate target for pre-population: "+target;
}
if (_.isFunction(target.loadData)) {
target.loadData(data);
} else if (_.isFunction(target.load)) {
target.load(data);
} else if (ko.isObservable(target)) {
target(data);
} else {
console.log("Warning: target for pre-populate is invalid");
target = target.data || target;
for (var prop in data) {
if (target.hasOwnProperty(prop)) {
var propTarget = target[prop];
if (_.isFunction(propTarget.loadData)) {
propTarget.loadData(data[prop]);
} else if (_.isFunction(propTarget.load)) {
propTarget.load(data[prop]);
} else if (ko.isObservable(propTarget)) {
propTarget(data[prop]);
} else {
console.log("Warning: target for pre-populate is invalid");
}
}
}


}); // This is a computed rather than a pureComputed as it has a side effect.
});
}
Expand Down
11 changes: 1 addition & 10 deletions grails-app/conf/example_models/behavioursExample.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,13 @@
"name": "param",
"type": "computed",
"expression": "item5"
},
{
"name": "item5",
"type": "computed",
"expression": "item5"
}
]
},
"mapping": [
{
"source-path": "param",
"target": "item6"
},
{
"source-path": "item5",
"target": "item5"
}
],
"target": "$data"
Expand Down Expand Up @@ -119,7 +110,7 @@
"items": [
{
"type": "literal",
"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."
"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."
}
]
}
Expand Down

0 comments on commit e23e490

Please sign in to comment.