Odoo12 开发之重写List记录打开方式

2019-01-31 4515点热度 0条评论

在Odoo中,记录的打开方式都是通过事件绑定的方式进行的,但要如何才能知道我在点击这个记录的时候触发了什么操作呢。这篇日志主要就是说明一下,在遇到这类情况的时候如何来找到正确的处理方式。

首先我们得到明确的需求,我们需要在Odoo的列表视图中重写打开记录指向的Form。

Odoo 列表视图

一、准备工作

进入Assets Debug模式

或者使用修改Url的方式

 http://localhost:8069/web?debug=assets#home

进入浏览器开发者模式(F12)

Chrome Debug

二、找寻需要的事件

事件管理

如果事件太多可以使用如下方式移除不必要的事件

接下来我们可以将不必要的实践移除后再点击行记录,如果能正确执行,那么代表我们需要的事件就还在这个列表中。

最终在我移除了大部分事件后,保留如下事件的情况下,功能还正常。那么可以断定,我需要的点击事件就是它。

跟进Js代码中,发现这个代码是这样的

proxy: function (method) {
        var self = this;
        return function () {
            var fn = (typeof method === 'string') ? self[method] : method;
            if (fn === void 0) {
                throw new Error("Couldn't find method '" + method + "' in widget " + self);
            }
            return fn.apply(self, arguments);
        };
    },

从代码中可以看出,这又是一个代理事件,那么继续打断点跟踪

最终在list_renderer.js文件见找到如何方法

    _onRowClicked: function (event) {
        // The special_click property explicitely allow events to bubble all
        // the way up to bootstrap's level rather than being stopped earlier.
        if (!$(event.target).prop('special_click')) {
            var id = $(event.currentTarget).data('id');
            if (id) {
                this.trigger_up('open_record', { id: id, target: event.target });
            }
        }
    }

再看方法引用

至此,就可以直接复写该方法来实现了。。

附上最后实现的代码~

odoo.define('ps.web.ListRenderer', function (require) {
"use strict";
var ListRenderer = require('web.ListRenderer');

ListRenderer.include({
		_onRowClicked: function (event) {
			if (this.state.model === 'combined.statements.working.paper.project') {
				let record = this.state.data.find(record => record.id === $(event.currentTarget).data('id')).data;
				this.do_action({
					type: "ir.actions.client",
					tag: 'working.papers',
					params: record
				});
			}else{
				this._super.apply(this, arguments);
			}
		}
	});
});

Jalena

原创内容,转载请注明出处! 部分内容来自网络,请遵守法律适用!

文章评论