[REF] web: RelationalModel: do not change resId in Record
With this commit, we ensure that the resId of a Record datapoint never changes. The only exception is for new records: their resId can go from `false` to their actual id when they are created. When we want to load another record, we must use the `load` function of the model, which now accepts `resId` (and `resIds`) as param, when the root is a "mono record". As a consequence, the instance of record datapoint now changes when using the pager. This required some changes in the AceField, to make it properly react in that case. We took the opportunity to simplify its implementation (several keys in the state were useless). Part of task~3179751 Part-of: odoo/odoo#114024
This commit is contained in:
committed by
Aaron Bohy
parent
65c8f41cf3
commit
3967dab0ce
@@ -153,7 +153,7 @@ export class Record extends DataPoint {
|
||||
resIds.splice(index, 1);
|
||||
const resId = resIds[Math.min(index, resIds.length - 1)] || false;
|
||||
if (resId) {
|
||||
await this._load({ resId, resIds });
|
||||
await this.model.load({ resId, resIds });
|
||||
} else {
|
||||
this.model._updateConfig(this.config, { resId: false }, { noReload: true });
|
||||
this.dirty = false;
|
||||
@@ -177,7 +177,7 @@ export class Record extends DataPoint {
|
||||
const resId = await this.model.orm.call(this.resModel, "copy", [this.resId], kwargs);
|
||||
const resIds = this.resIds.slice();
|
||||
resIds.splice(index + 1, 0, resId);
|
||||
await this._load({ resId, resIds, mode: "edit" });
|
||||
await this.model.load({ resId, resIds, mode: "edit" });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -201,12 +201,11 @@ export class Record extends DataPoint {
|
||||
return this._invalidFields.has(fieldName);
|
||||
}
|
||||
|
||||
load(resId = this.resId, context = this.context) {
|
||||
let mode = this.config.mode;
|
||||
if (!resId) {
|
||||
mode = "edit";
|
||||
load() {
|
||||
if (arguments.length > 0) {
|
||||
throw new Error("Record.load() does not accept arguments");
|
||||
}
|
||||
return this.model.mutex.exec(() => this._load({ resId, mode, context }));
|
||||
return this.model.mutex.exec(() => this._load());
|
||||
}
|
||||
|
||||
openInvalidFieldsNotification() {
|
||||
@@ -540,8 +539,9 @@ export class Record extends DataPoint {
|
||||
}
|
||||
|
||||
async _load(nextConfig = {}) {
|
||||
// FIXME: do not allow to change resId? maybe add a new method on model to re-generate a
|
||||
// new root for the new resId
|
||||
if ("resId" in nextConfig && this.resId) {
|
||||
throw new Error("Cannot change resId of a record");
|
||||
}
|
||||
await this.model._updateConfig(this.config, nextConfig, {
|
||||
commit: (values) => {
|
||||
if (this.resId) {
|
||||
@@ -767,13 +767,18 @@ export class Record extends DataPoint {
|
||||
return onError(e, { discard: () => this._discard() });
|
||||
}
|
||||
if (!this.isInEdition) {
|
||||
await this._load({ resId });
|
||||
const nextConfig = {};
|
||||
if (creation) {
|
||||
nextConfig.resId = resId;
|
||||
}
|
||||
await this._load(nextConfig);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (!noReload) {
|
||||
const nextConfig = { resId };
|
||||
const nextConfig = {};
|
||||
if (creation) {
|
||||
nextConfig.resId = resId;
|
||||
nextConfig.resIds = this.resIds.concat([resId]);
|
||||
}
|
||||
await this._load(nextConfig);
|
||||
@@ -857,13 +862,13 @@ export class Record extends DataPoint {
|
||||
*/
|
||||
async _toggleArchive(state) {
|
||||
const method = state ? "action_archive" : "action_unarchive";
|
||||
const context = this.context;
|
||||
const resId = this.resId;
|
||||
const action = await this.model.orm.call(this.resModel, method, [[resId]], { context });
|
||||
const action = await this.model.orm.call(this.resModel, method, [[this.resId]], {
|
||||
context: this.context,
|
||||
});
|
||||
if (action && Object.keys(action).length) {
|
||||
this.model.action.doAction(action, { onClose: () => this._load({ resId }) });
|
||||
this.model.action.doAction(action, { onClose: () => this._load() });
|
||||
} else {
|
||||
return this._load({ resId });
|
||||
return this._load();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -248,7 +248,13 @@ export class RelationalModel extends Model {
|
||||
const config = Object.assign({}, currentConfig);
|
||||
|
||||
config.context = "context" in params ? params.context : config.context;
|
||||
if (!currentConfig.isMonoRecord) {
|
||||
if (currentConfig.isMonoRecord) {
|
||||
config.resId = "resId" in params ? params.resId : config.resId;
|
||||
config.resIds = "resIds" in params ? params.resIds : config.resIds;
|
||||
if (!config.resId && config.mode !== "edit") {
|
||||
config.mode = "edit";
|
||||
}
|
||||
} else {
|
||||
config.domain = "domain" in params ? params.domain : config.domain;
|
||||
config.comparison = "comparison" in params ? params.comparison : config.comparison;
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
import { _lt } from "@web/core/l10n/translation";
|
||||
import { registry } from "@web/core/registry";
|
||||
import { useBus, useService } from "@web/core/utils/hooks";
|
||||
import { effect } from "@web/core/utils/reactive";
|
||||
import { standardFieldProps } from "../standard_field_props";
|
||||
|
||||
import { CodeEditor } from "@web/core/code_editor/code_editor";
|
||||
import { Component, useState } from "@odoo/owl";
|
||||
import { useRecordObserver } from "@web/model/relational_model/utils";
|
||||
import { useDebounced } from "@web/core/utils/timing";
|
||||
|
||||
export class AceField extends Component {
|
||||
@@ -25,24 +25,11 @@ export class AceField extends Component {
|
||||
this.cookies = useService("cookie");
|
||||
this.debouncedCommit = useDebounced(this.commitChanges, 5000);
|
||||
|
||||
this.state = useState({
|
||||
onChange: (value) => this.handleChange(value),
|
||||
class: "ace-view-editor",
|
||||
theme: this.theme,
|
||||
this.state = useState({});
|
||||
useRecordObserver((record) => {
|
||||
this.state.value = record.data[this.props.name];
|
||||
});
|
||||
|
||||
this.lastSetValue = false;
|
||||
effect(
|
||||
async ({ record, mode, name, readonly }) => {
|
||||
if (this.lastSetValue !== record.data[name]) {
|
||||
this.state.value = record.data[name];
|
||||
}
|
||||
this.state.mode = mode === "xml" ? "qweb" : mode;
|
||||
this.state.readonly = readonly;
|
||||
},
|
||||
[this.props]
|
||||
);
|
||||
|
||||
const { model } = this.props.record;
|
||||
useBus(model.bus, "WILL_SAVE_URGENTLY", () => this.commitChanges());
|
||||
useBus(model.bus, "NEED_LOCAL_CHANGES", ({ detail }) =>
|
||||
@@ -50,6 +37,9 @@ export class AceField extends Component {
|
||||
);
|
||||
}
|
||||
|
||||
get mode() {
|
||||
return this.props.mode === "xml" ? "qweb" : this.props.mode;
|
||||
}
|
||||
get theme() {
|
||||
return this.cookies.current.color_scheme === "dark" ? "monokai" : "";
|
||||
}
|
||||
@@ -64,7 +54,6 @@ export class AceField extends Component {
|
||||
if (!this.props.readonly) {
|
||||
const value = this.state.value;
|
||||
if (this.props.record.data[this.props.name] !== value) {
|
||||
this.lastSetValue = value;
|
||||
await this.props.record.update({ [this.props.name]: value });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
<div class="o_field_widget oe_form_field o_ace_view_editor oe_ace_open">
|
||||
<CodeEditor
|
||||
value="state.value"
|
||||
mode="state.mode"
|
||||
readonly="state.readonly"
|
||||
mode="mode"
|
||||
readonly="props.readonly"
|
||||
onChange.bind="handleChange"
|
||||
class="'ace-view-editor'"
|
||||
theme="theme"
|
||||
|
||||
@@ -325,7 +325,7 @@ export class FormController extends Component {
|
||||
});
|
||||
}
|
||||
if (canProceed) {
|
||||
return this.model.root.load(resIds[offset]);
|
||||
return this.model.load({ resId: resIds[offset] });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -502,7 +502,7 @@ export class FormController extends Component {
|
||||
// FIXME: disable/enable not done in onPagerUpdate
|
||||
if (canProceed) {
|
||||
const btns = this.disableButtons();
|
||||
await this.model.root.load(false);
|
||||
await this.model.load({ resId: false });
|
||||
this.enableButtons(btns);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ class KanbanQuickCreateController extends Component {
|
||||
if (resId) {
|
||||
await this.props.onValidate(resId, mode);
|
||||
if (mode === "add") {
|
||||
await this.model.root.load(false);
|
||||
await this.model.load({ resId: false });
|
||||
}
|
||||
}
|
||||
this.state.disabled = false;
|
||||
|
||||
@@ -133,7 +133,7 @@ class _Record extends Component {
|
||||
if (nextProps.values) {
|
||||
return loadWithValues(nextProps.values);
|
||||
} else if (nextProps.info.resId !== this.model.root.resId) {
|
||||
return this.model.root.load(nextProps.info.resId);
|
||||
return this.model.load({ resId: nextProps.info.resId });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,8 +45,7 @@ export class FormViewDialog extends Component {
|
||||
delete context[k];
|
||||
}
|
||||
});
|
||||
await record.load(false, context);
|
||||
// await record.model.load({ resId: null, context }); // FIXME context
|
||||
await record.model.load({ resId: false, context });
|
||||
} else {
|
||||
this.props.close();
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ QUnit.module("Fields", (hooks) => {
|
||||
|
||||
await pagerNext(target);
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
|
||||
assert.ok(target.querySelector(".o_field_code").textContent.includes("blip"));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user