Solve the problem of the buttons stopping instead of refreshing

This commit is contained in:
Mazen Abdo 2025-05-21 14:42:34 +03:00
parent bf7c6deda6
commit 4df025b433
1 changed files with 63 additions and 47 deletions

View File

@ -1,50 +1,66 @@
odoo.define('to_safe_confirm_button.safe_confirm', function (require) { odoo.define("to_safe_confirm_button.safe_confirm", function (require) {
"use strict"; "use strict";
var Dialog = require('web.Dialog') var Dialog = require("web.Dialog");
var form_controller = require('web.FormController') var form_controller = require("web.FormController");
form_controller.include({ form_controller.include({
_onButtonClicked: function (event) { /**
event.stopPropagation(); * @private
* @param {OdooEvent} ev
*/
_onButtonClicked: function (ev) {
// stop the event's propagation as a form controller might have other
// form controllers in its descendants (e.g. in a FormViewDialog)
ev.stopPropagation();
var self = this; var self = this;
var def; var def;
this._disableButtons(); this._disableButtons();
function saveAndExecuteAction () {
return self.saveRecord(self.handle, { function saveAndExecuteAction() {
return self
.saveRecord(self.handle, {
stayInEdit: true, stayInEdit: true,
}).then(function () { })
var record = self.model.get(event.data.record.id); .then(function () {
// we need to reget the record to make sure we have changes made
// by the basic model, such as the new res_id, if the record is
// new.
var record = self.model.get(ev.data.record.id);
return self._callButtonAction(attrs, record); return self._callButtonAction(attrs, record);
}); });
} }
var attrs = event.data.attrs; var attrs = ev.data.attrs;
if (attrs.confirm) { if (attrs.confirm) {
var d = $.Deferred(); def = new Promise(function (resolve, reject) {
Dialog.confirm(this, attrs.confirm, { Dialog.confirm(self, attrs.confirm, {
confirm_callback: saveAndExecuteAction, confirm_callback: saveAndExecuteAction,
}).on("closed", null, function () { }).on("closed", null, resolve);
d.resolve();
}); });
def = d.promise(); } else if (attrs.safe_confirm) {
} def = new Promise(function (resolve, reject) {
else if (attrs.safe_confirm){ Dialog.safeConfirm(self, attrs.confirm, {
var d = $.Deferred();
Dialog.safeConfirm(this, attrs.safe_confirm, {
confirm_callback: saveAndExecuteAction, confirm_callback: saveAndExecuteAction,
}).on("closed", null, function () { }).on("closed", null, resolve);
d.resolve();
}); });
def = d.promise(); } else if (attrs.special === "cancel") {
} def = this._callButtonAction(attrs, ev.data.record);
else if (attrs.special === 'cancel') { } else if (!attrs.special || attrs.special === "save") {
def = this._callButtonAction(attrs, event.data.record);
} else if (!attrs.special || attrs.special === 'save') {
// save the record but don't switch to readonly mode // save the record but don't switch to readonly mode
def = saveAndExecuteAction(); def = saveAndExecuteAction();
} else {
console.warn("Unhandled button event", ev);
return;
} }
def.then(this._enableButtons.bind(this)); // Kind of hack for FormViewDialog: button on footer should trigger the dialog closing
// if the `close` attribute is set
def.then(function () {
self._enableButtons();
if (attrs.close) {
self.trigger_up("close_dialog");
}
}).guardedCatch(this._enableButtons.bind(this));
}, },
});
}); });
});