扩展方法不起作用
Extended method not working
这是我以前的。现在我创建了一个扩展原始方法的 do_delete
方法。我也将其包含在 views.xml
中
<template id="assets_backend" name="amgl assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript"
src="/amgl/static/src/js/amgl.js">
</script>
</xpath>
</template>
但我面临的问题是。它仅在我启用 debug=assets
时有效,我最近才发现它甚至无法在正常 debug=1
模式下工作。
编辑
amgl.js
function join_name(names) {
var str = "";
var step;
for (step = 0; step < names.length; step++) {
str = str + (step + 1) + '- ' + names[step].full_name + ' \n';
};
return str;
}
odoo.define('amgl.web.ListView', function (require) {
"use strict";
var core = require('web.core');
var _t = core._t;
var Model = require('web.DataModel');
var ListView = require('web.ListView');
var ListViewDeleteExtension = ListView.include({
do_delete: function (ids) {
if (this.model == 'amgl.customer') {
var self = this;
new Model(self.model).call('read', [ids, ['full_name'], this.dataset.get_context()])
.done(function (names) {
var text = _t("Do you really want to remove these records?") + ' \n \n' + join_name(names)
if (!(ids.length && confirm(text))) {
return;
}
return $.when(self.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
});
}
else {
if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
return;
}
var self = this;
return $.when(this.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
}
},
});
});
编辑 2
这是我包含在清单中的完整 xml 文件。
<odoo>
<data>
<template id="assets_backend" name="amgl assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript"
src="/amgl/static/src/js/amgl.js">
</script>
</xpath>
</template>
<template id="assets_backend1"
name="web_duplicate_visibility backend assets"
inherit_id="web.assets_backend">
<xpath expr="."
position="inside">
<script type="text/javascript"
src="/amgl/static/src/js/duplicate_visibility.js">
</script>
</xpath>
</template>
</data>
为了使其工作,您必须扩展保存在 core.view_registry 中的 class:
function join_name(names) {
var str = "";
var step;
for (step = 0; step < names.length; step++) {
str = str + (step + 1) + '- ' + names[step].full_name+ ' \n';
};
return str;
}
odoo.define('amgl.web.ListView', function (require) {
"use strict";
// put an alert here and refresh the page if the alert don't appear
// than the javascript file is not loaded at all you need to upgrade
// you module
alert(" hi i'm loaded and my code is executed?!!!");
var core = require('web.core');
var _t = core._t;
var Model = require('web.DataModel');
// the Class is saved in view_registry
// Note same thing for widgets,widgets for form view are saved in : form_widget_registry
// always look for the key and get them using that key
// in odoo 10.0 if you look at list_view.js you will find this line:
// core.view_registry.add('list', ListView);
// here he saved the class with key 'list'
var ListView = core.view_registry.get('list');
// just override the do_delete methd
ListView.include({
do_delete: function (ids) {
console.log('override the function don again'); // to make sure
if (this.model == 'amgl.customer') {
var self = this;
new Model(self.model).call('read', [ids, ['full_name'], this.dataset.get_context()])
.done(function (names) {
var text = _t("Do you really want to remove these records?") + ' \n \n' + join_name(names)
if (!(ids.length && confirm(text))) {
return;
}
return $.when(self.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
});
}
else {
if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
return;
}
var self = this;
return $.when(this.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
}
},
});
});
始终记得通过扩展 backend_assets 模板来加载 JS 字段:
<!--We need to load our js file to backend_assets-->
<template id="assets_backend" name="costum assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/amgl/static/src/js/amgl.js"></script>
<script type="text/javascript"
src="/amgl/static/src/js/duplicate_visibility.js">
</script>
</xpath>
</template>
并将此文件添加到清单
'data': [
'your_xml_file_name.xml',
],
它对我有用,消息显示在控制台上。你一定是做错了什么
出于测试目的,我将代码更改为获取姓名而不是全名,因此它可以按预期与销售团队模型一起使用。
这是我以前的do_delete
方法。我也将其包含在 views.xml
<template id="assets_backend" name="amgl assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript"
src="/amgl/static/src/js/amgl.js">
</script>
</xpath>
</template>
但我面临的问题是。它仅在我启用 debug=assets
时有效,我最近才发现它甚至无法在正常 debug=1
模式下工作。
编辑
amgl.js
function join_name(names) {
var str = "";
var step;
for (step = 0; step < names.length; step++) {
str = str + (step + 1) + '- ' + names[step].full_name + ' \n';
};
return str;
}
odoo.define('amgl.web.ListView', function (require) {
"use strict";
var core = require('web.core');
var _t = core._t;
var Model = require('web.DataModel');
var ListView = require('web.ListView');
var ListViewDeleteExtension = ListView.include({
do_delete: function (ids) {
if (this.model == 'amgl.customer') {
var self = this;
new Model(self.model).call('read', [ids, ['full_name'], this.dataset.get_context()])
.done(function (names) {
var text = _t("Do you really want to remove these records?") + ' \n \n' + join_name(names)
if (!(ids.length && confirm(text))) {
return;
}
return $.when(self.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
});
}
else {
if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
return;
}
var self = this;
return $.when(this.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
}
},
});
});
编辑 2
这是我包含在清单中的完整 xml 文件。
<odoo>
<data>
<template id="assets_backend" name="amgl assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript"
src="/amgl/static/src/js/amgl.js">
</script>
</xpath>
</template>
<template id="assets_backend1"
name="web_duplicate_visibility backend assets"
inherit_id="web.assets_backend">
<xpath expr="."
position="inside">
<script type="text/javascript"
src="/amgl/static/src/js/duplicate_visibility.js">
</script>
</xpath>
</template>
</data>
为了使其工作,您必须扩展保存在 core.view_registry 中的 class:
function join_name(names) {
var str = "";
var step;
for (step = 0; step < names.length; step++) {
str = str + (step + 1) + '- ' + names[step].full_name+ ' \n';
};
return str;
}
odoo.define('amgl.web.ListView', function (require) {
"use strict";
// put an alert here and refresh the page if the alert don't appear
// than the javascript file is not loaded at all you need to upgrade
// you module
alert(" hi i'm loaded and my code is executed?!!!");
var core = require('web.core');
var _t = core._t;
var Model = require('web.DataModel');
// the Class is saved in view_registry
// Note same thing for widgets,widgets for form view are saved in : form_widget_registry
// always look for the key and get them using that key
// in odoo 10.0 if you look at list_view.js you will find this line:
// core.view_registry.add('list', ListView);
// here he saved the class with key 'list'
var ListView = core.view_registry.get('list');
// just override the do_delete methd
ListView.include({
do_delete: function (ids) {
console.log('override the function don again'); // to make sure
if (this.model == 'amgl.customer') {
var self = this;
new Model(self.model).call('read', [ids, ['full_name'], this.dataset.get_context()])
.done(function (names) {
var text = _t("Do you really want to remove these records?") + ' \n \n' + join_name(names)
if (!(ids.length && confirm(text))) {
return;
}
return $.when(self.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
});
}
else {
if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
return;
}
var self = this;
return $.when(this.dataset.unlink(ids)).done(function () {
_(ids).each(function (id) {
self.records.remove(self.records.get(id));
});
// Hide the table if there is no more record in the dataset
if (self.display_nocontent_helper()) {
self.no_result();
} else {
if (self.records.length && self.current_min === 1) {
// Reload the list view if we delete all the records of the first page
self.reload();
} else if (self.records.length && self.dataset.size() > 0) {
// Load previous page if the current one is empty
self.pager.previous();
}
// Reload the list view if we are not on the last page
if (self.current_min + self._limit - 1 < self.dataset.size()) {
self.reload();
}
}
self.update_pager(self.dataset);
self.compute_aggregates();
});
}
},
});
});
始终记得通过扩展 backend_assets 模板来加载 JS 字段:
<!--We need to load our js file to backend_assets-->
<template id="assets_backend" name="costum assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/amgl/static/src/js/amgl.js"></script>
<script type="text/javascript"
src="/amgl/static/src/js/duplicate_visibility.js">
</script>
</xpath>
</template>
并将此文件添加到清单
'data': [
'your_xml_file_name.xml',
],
它对我有用,消息显示在控制台上。你一定是做错了什么
出于测试目的,我将代码更改为获取姓名而不是全名,因此它可以按预期与销售团队模型一起使用。