【问题标题】:How to perform some action on a node after it is loaded in JavaScript in Odoo?在 Odoo 中的 JavaScript 中加载节点后如何在节点上执行一些操作?
【发布时间】:2018-09-10 08:05:45
【问题描述】:

我为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');
    },
})

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

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

【问题讨论】:

    标签: javascript xml odoo odoo-10


    【解决方案1】:

    我可以使用start 方法,它也会自动调用。这发生在调用initwillStart 之后,然后渲染视图,然后调用start。这在官方文档的 Widget Lifecycle 部分中有很好的解释:

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

    渲染完成后,框架会自动调用 启动方法。这对于执行一些专门的 渲染后的工作。例如,建立图书馆。

    必须返回一个 deferred 以指示其工作何时完成。

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

    start: function() {
        console.log('START');
        this._super.apply(this, arguments);
        this.$el.parent().find('input').val('START');
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多