如何在 js 文件中覆盖 odoo 9
How override in js file odoo 9
我需要在下面的函数 ASC 到 DESC 中覆盖
render: function (messages, options) {
clearTimeout(this.auto_render_timeout);
var self = this;
var msgs = _.map(messages, this._preprocess_message.bind(this));
if (this.options.display_order === ORDER.ASC) {
msgs.reverse();
}
您需要替换函数,然后调用_super()。在实现此功能的 class 中包含您的方法。 发布了一个类似的问题,其中包含经过测试的特定实现。我的以下示例尚未经过全面测试。请记住安装您的插件并更新它以查看您的更改。
}
'name': "asc_desc",
'summary': """
Short (1 phrase/line) summary of the module's purpose, used as
subtitle on modules listing or apps.openerp.com""",
'description': """
Long description of module's purpose
""",
'author': "My Company",
'website': "http://www.yourcompany.com",
# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['base'],
# always loaded
'data': [
# 'security/ir.model.access.csv',
'views.xml',
'templates.xml',
'javascript_import.xml',
],
# only loaded in demonstration mode
'demo': [
'demo.xml',
],
}
javascript_import.xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<template id="assets_backend_asc_desc" name="assets_backend_asc_desc" inherit_id="web.assets_backend">
<xpath expr="script[last()]" position="after">
<script type="text/javascript" src="/asc_desc/static/src/js/asc.js"></script>
</xpath>
</template>
</data>
</openerp>
asc.js
odoo.define('asc_desc.ChatThread', function(require){
"use strict"
var core = require('web.core');
var QWeb = core.qweb;
var thread = require('mail.ChatThread');
var ORDER = {
DESC: -1,
ASC: 1,
}
thread.include({
render: function (messages, options) {
console.log("TEST");
var msgs = _.map(messages, this._preprocess_message.bind(this));
if (this.options.display_order === ORDER.DESC) {
msgs.reverse();
}
options = _.extend({}, this.options, options);
// Hide avatar and info of a message if that message and the previous
// one are both comments wrote by the same author at the same minute
var prev_msg;
_.each(msgs, function (msg) {
if (!prev_msg || (Math.abs(msg.date.diff(prev_msg.date)) > 60000) || prev_msg.message_type !== 'comment' || msg.message_type !== 'comment' || (prev_msg.author_id[0] !== msg.author_id[0])) {
msg.display_author = true;
} else {
msg.display_author = !options.squash_close_messages;
}
prev_msg = msg;
});
this.$el.html(QWeb.render('mail.ChatThread', {
messages: msgs,
options: options,
ORDER: ORDER,
}));
},
});
});
确保在 __openerp__.py
文件中包含声明 js 的文件。
我需要在下面的函数 ASC 到 DESC 中覆盖
render: function (messages, options) {
clearTimeout(this.auto_render_timeout);
var self = this;
var msgs = _.map(messages, this._preprocess_message.bind(this));
if (this.options.display_order === ORDER.ASC) {
msgs.reverse();
}
您需要替换函数,然后调用_super()。在实现此功能的 class 中包含您的方法。
}
'name': "asc_desc",
'summary': """
Short (1 phrase/line) summary of the module's purpose, used as
subtitle on modules listing or apps.openerp.com""",
'description': """
Long description of module's purpose
""",
'author': "My Company",
'website': "http://www.yourcompany.com",
# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['base'],
# always loaded
'data': [
# 'security/ir.model.access.csv',
'views.xml',
'templates.xml',
'javascript_import.xml',
],
# only loaded in demonstration mode
'demo': [
'demo.xml',
],
}
javascript_import.xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<template id="assets_backend_asc_desc" name="assets_backend_asc_desc" inherit_id="web.assets_backend">
<xpath expr="script[last()]" position="after">
<script type="text/javascript" src="/asc_desc/static/src/js/asc.js"></script>
</xpath>
</template>
</data>
</openerp>
asc.js
odoo.define('asc_desc.ChatThread', function(require){
"use strict"
var core = require('web.core');
var QWeb = core.qweb;
var thread = require('mail.ChatThread');
var ORDER = {
DESC: -1,
ASC: 1,
}
thread.include({
render: function (messages, options) {
console.log("TEST");
var msgs = _.map(messages, this._preprocess_message.bind(this));
if (this.options.display_order === ORDER.DESC) {
msgs.reverse();
}
options = _.extend({}, this.options, options);
// Hide avatar and info of a message if that message and the previous
// one are both comments wrote by the same author at the same minute
var prev_msg;
_.each(msgs, function (msg) {
if (!prev_msg || (Math.abs(msg.date.diff(prev_msg.date)) > 60000) || prev_msg.message_type !== 'comment' || msg.message_type !== 'comment' || (prev_msg.author_id[0] !== msg.author_id[0])) {
msg.display_author = true;
} else {
msg.display_author = !options.squash_close_messages;
}
prev_msg = msg;
});
this.$el.html(QWeb.render('mail.ChatThread', {
messages: msgs,
options: options,
ORDER: ORDER,
}));
},
});
});
确保在 __openerp__.py
文件中包含声明 js 的文件。