在Odoo中加载JavaScript后,如何在节点上执行某些操作?

问题描述 投票:0回答:1

我为Char领域制作了一个新的小部件。我将从Char的原始模板继承其模板:

<t t-name="FieldRed" t-extend="FieldChar">
    <t t-jquery="input" t-operation="attributes">
        <attribute name="id">barcode_input</attribute>
        <attribute name="class">o_form_input bg-red</attribute>
    </t>
</t>

然后,我只是尝试在加载时用一些文本填充字段(我知道我可以在不使用JavaScript的情况下执行此操作,但我需要管理它以从我的实际目的开始)。所以,我这样做了:

var FieldRed = widget.FieldChar.extend({
    template: 'FieldRed',
    events: _.extend({}, widget.FieldChar.prototype.events, {
        'load': 'on_load',
        'ready': 'on_ready',
        'keypress': 'on_keypress',
    }),

    init: function (field_manager, node) {
        console.log('INIT');
        this._super(field_manager, node);
        this.$el.parent().find('input').val('INIT');
    },

    on_load: function (e) {
        console.log('LOAD');
        this.$el.parent().find('input').val('LOAD');
    },

    on_ready: function (e) {
        console.log('READY');
        this.$el.parent().find('input').val('READY');
    },

    on_keypress: function (e) {
        console.log('KEYPRESS');
        this.$el.parent().find('input').val('KEYPRESS');
    },
})

首先,我期望在input找到this.$el元素,但它在this.$el.parent(),是因为input元素在原始模板中声明?

我的主要问题是:我可以在input上自动填写keypress节点文本,但是同样的代码行在init方法中不起作用,既不是load也不是ready事件。每次用户打开包含我的小部件字段的表单时,如何填写input节点的文本?

javascript xml odoo odoo-10
1个回答
0
投票

我可以使用start方法,也可以自动调用。这发生在调用initwillStart之后,然后渲染视图,然后调用start。官方文档中的Widget生命周期部分对此进行了详细说明:

https://www.odoo.com/documentation/11.0/reference/javascript_reference.html

渲染完成后,框架将自动调用start方法。这对于执行一些专门的后期渲染工作很有用。例如,设置库。

必须返回延迟以指示其工作何时完成。

所以我只需继承原始的start方法并添加我的功能:

start: function() {
    console.log('START');
    this._super.apply(this, arguments);
    this.$el.parent().find('input').val('START');
},
© www.soinside.com 2019 - 2024. All rights reserved.