odoo - 扩展方法不起作用

标签 odoo odoo-view

这是我以前的 question 。现在我创建了一个 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 文件,我将其包含在 list 中。

<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 中的类:

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>

并将此文件添加到 list

'data': [
  'your_xml_file_name.xml',
],

这对我有用,消息显示在控制台上。你肯定做错了什么

出于测试目的,我将代码更改为获取名称而不是全名,因此它可以按预期与销售团队模型配合使用。

enter image description here

关于odoo - 扩展方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46917254/

相关文章:

python - 卢比符号在 odoo 的发票报告中显示为空白框

python - 如何创建在单击按钮时显示动态数据的 View

python - 有没有办法在 Odoo 11 中动态地将字段添加到 TreeView 中?

odoo-8 - Odoo - 更改 one2many TreeView 中的特定列颜色

odoo - 在 Odoo 9 的销售订单表单 View 中隐藏 "Confirm Sale"按钮

python-2.7 - 如何更改 SaleOrder 的名称格式?奥杜10

odoo-8 - 在 TreeView Odoo 8 中添加按钮

python - 如何自定义发票打印 odoo 10

ios - 如何在 ODOO 中的特定客户(res.partner)下创建任务(project.task)?

Odoo 8 - 如何创建一个包含弹出消息并刷新实际 View 的按钮?