在 Odoo 的 JavaScript 中加载节点后,如何对节点执行某些操作?
How to perform some action on a node after it is loaded in JavaScript in Odoo?
我为 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
方法中不起作用,load
或 ready
事件。每次用户打开包含我的小部件字段的表单时,如何填写 input
节点的文本?
我可以使用start
方法,这个方法也是自动调用的。这发生在调用 init
和 willStart
之后,然后呈现视图,然后调用 start
。这在官方文档的 Widget Lifecycle 部分中有很好的解释:
https://www.odoo.com/documentation/11.0/reference/javascript_reference.html
When the rendering is complete, the framework will automatically call
the start method. This is useful to perform some specialized
post-rendering work. For example, setting up a library.
Must return a deferred to indicate when its work is done.
所以我只需要继承原来的start
方法并添加我的功能:
start: function() {
console.log('START');
this._super.apply(this, arguments);
this.$el.parent().find('input').val('START');
},
我为 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
方法中不起作用,load
或 ready
事件。每次用户打开包含我的小部件字段的表单时,如何填写 input
节点的文本?
我可以使用start
方法,这个方法也是自动调用的。这发生在调用 init
和 willStart
之后,然后呈现视图,然后调用 start
。这在官方文档的 Widget Lifecycle 部分中有很好的解释:
https://www.odoo.com/documentation/11.0/reference/javascript_reference.html
When the rendering is complete, the framework will automatically call the start method. This is useful to perform some specialized post-rendering work. For example, setting up a library.
Must return a deferred to indicate when its work is done.
所以我只需要继承原来的start
方法并添加我的功能:
start: function() {
console.log('START');
this._super.apply(this, arguments);
this.$el.parent().find('input').val('START');
},