扩展方法不起作用

时间:2017-10-24 18:07:37

标签: openerp 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文件。

<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>

1 个答案:

答案 0 :(得分:2)

为了使其工作,您必须扩展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>

并将此文件添加到清单

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

它对我有用,消息显示在控制台上。你必须做错事

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

enter image description here