修补现有的 js 函数但仍然显示 [英] Patching an existing js function but still show up
问题描述
我使用odoo11.我有一个需求,就是在点击删除时更改确认内容.
我从 web 插件上名为 basic_controller.js
的 js 文件中找到了这个
var BasicController = AbstractController.extend(FieldManagerMixin, {..._deleteRecords:函数(ID){var self = this;函数 doIt() {返回 self.model.deleteRecords(ids, self.modelName).then(self._onDeletedRecords.bind(self, ids));}如果(this.confirmOnDelete){Dialog.confirm(this, _t("你确定要删除这条记录吗?"), {确认回调:做它,});} 别的 {做它();}},...
我从
如何完全替换原点?
如果您覆盖函数的整个内容.那么你就不需要调用 this._super.apply(this, arguments);
目前,它会在您之前创建一个对话框.
JS文件
odoo.define('mytest.field', function (require) {'使用严格';var basic_fields = require('web.BasicController');var Dialog = require('web.Dialog');var core = require('web.core');var _t = core._t;var FieldClearbit = basic_fields.include({_deleteRecords:函数(ID){var self = this;函数 doIt() {返回 self.model.deleteRecords(ids, self.modelName).then(self._onDeletedRecords.bind(self, ids));}如果(this.confirmOnDelete){Dialog.confirm(this, _t(重写内容?"), {确认回调:做它,});} 别的 {做它();}},})});
XML 文件
<odoo><模板id="assets_backend";名称=Rdets"inherit_id="web.assets_backend"><xpath expr=".";位置=内部"><script type="text/javascript";src="/mytest/static/src/js/test.js";/></xpath>模板></odoo>
尝试启用资产调试以重新生成网络资产.您很可能需要升级模块以应用更改.
I use odoo11. I have a requirement that is to change the confirm content when click delete.
I found this from a js file called basic_controller.js
on web addons
var BasicController = AbstractController.extend(FieldManagerMixin, {
...
_deleteRecords: function (ids) {
var self = this;
function doIt() {
return self.model
.deleteRecords(ids, self.modelName)
.then(self._onDeletedRecords.bind(self, ids));
}
if (this.confirmOnDelete) {
Dialog.confirm(this, _t("Are you sure you want to delete this record ?"), {
confirm_callback: doIt,
});
} else {
doIt();
}
},
...
And I found the patching method from here. I write a new js file to patch it like this.
BasicController.include({
_deleteRecords: function (ids) {
this._super.apply(this, arguments);
var self = this;
function doIt() {
return self.model.deleteRecords(ids, self.modelName).then(self._onDeletedRecords.bind(self, ids));
}
if (this.confirmOnDelete) {
Dialog.confirm(this, _t("rewrite content?"), {
confirm_callback: doIt,
});
} else {
doIt();
}
},
})
It will show my content but after confirm or cancel. The origin one will follow up.
How can I replace the origin one totally?
if you overwrite functions whole content. Then you don't need to call this._super.apply(this, arguments);
Currently it creates one dialog before yours.
JS File
odoo.define('mytest.field', function (require) {
'use strict';
var basic_fields = require('web.BasicController');
var Dialog = require('web.Dialog');
var core = require('web.core');
var _t = core._t;
var FieldClearbit = basic_fields.include({
_deleteRecords: function (ids) {
var self = this;
function doIt() {
return self.model.deleteRecords(ids, self.modelName).then(self._onDeletedRecords.bind(self, ids));
}
if (this.confirmOnDelete) {
Dialog.confirm(this, _t("rewrite content?"), {
confirm_callback: doIt,
});
} else {
doIt();
}
},
})
});
XML File
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="assets_backend" name="Rdets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/mytest/static/src/js/test.js" />
</xpath>
</template>
</odoo>
Try enabling assets debugging to regenerate web assets. Most likely you need to upgrade your module to changes to apply.
这篇关于修补现有的 js 函数但仍然显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!