[IMP] web: handle before/after saved Record
This commit adds to the Record Component, the possibility to pass the onWillSaveRecord/onRecordSaved props that will be triggered before the save of the Record and after it respectively. closes odoo/odoo#120448 Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
This commit is contained in:
@@ -15,6 +15,8 @@ class _Record extends Component {
|
||||
viewMode: "form",
|
||||
rootType: "record",
|
||||
activeFields: this.getActiveFields(),
|
||||
onRecordSaved: this.props.info.onRecordSaved || (() => {}),
|
||||
onWillSaveRecord: this.props.info.onWillSaveRecord || (() => {}),
|
||||
};
|
||||
const modelServices = Object.fromEntries(
|
||||
RelationalModel.services.map((servName) => {
|
||||
@@ -107,4 +109,6 @@ Record.props = [
|
||||
"mode?",
|
||||
"values?",
|
||||
"onRecordChanged?",
|
||||
"onRecordSaved?",
|
||||
"onWillSaveRecord?",
|
||||
];
|
||||
|
||||
@@ -240,6 +240,43 @@ QUnit.module("Record Component", (hooks) => {
|
||||
assert.strictEqual(target.querySelector("[name='foo'] input").value, "753");
|
||||
});
|
||||
|
||||
QUnit.test("provides a way to handle before/after saved the record", async function (assert) {
|
||||
class Parent extends Component {
|
||||
onRecordSaved(record) {
|
||||
assert.step("onRecordSaved");
|
||||
}
|
||||
|
||||
onWillSaveRecord(record) {
|
||||
assert.step("onWillSaveRecord");
|
||||
}
|
||||
}
|
||||
Parent.components = { Record, Field };
|
||||
Parent.template = xml`
|
||||
<Record resModel="'partner'" resId="1" fieldNames="['foo']" mode="'edit'" t-slot-scope="data" onRecordSaved="onRecordSaved" onWillSaveRecord="onWillSaveRecord">
|
||||
<button class="save" t-on-click="() => data.record.save()">Save</button>
|
||||
<Field name="'foo'" record="data.record"/>
|
||||
</Record>`;
|
||||
|
||||
const env = await makeTestEnv({
|
||||
serverData,
|
||||
mockRPC(route, args) {
|
||||
assert.step(args.method);
|
||||
},
|
||||
});
|
||||
await mount(Parent, target, { env });
|
||||
|
||||
await editInput(target, "[name='foo'] input", "abc");
|
||||
await click(target, "button.save");
|
||||
assert.verifySteps([
|
||||
"fields_get",
|
||||
"read",
|
||||
"onWillSaveRecord",
|
||||
"write",
|
||||
"read",
|
||||
"onRecordSaved",
|
||||
]);
|
||||
});
|
||||
|
||||
QUnit.test("handles many2one fields", async function (assert) {
|
||||
patchWithCleanup(browser, {
|
||||
setTimeout: (fn) => fn(),
|
||||
|
||||
Reference in New Issue
Block a user