Javascript - 在 parent 上调用 super parent?
Javascript - call super on parents parent?
我已经在 Odoo 中定义了当前自定义 javascript 视图的扩展:
openerp.account_move_journal_test = function(instance){
var _t = instance.web._t,
_lt = instance.web._lt;
var QWeb = instance.web.qweb;
instance.web.account.QuickAddListView.include({
init: function(){
this._super.apply(this, arguments);
console.log("QuickAddListView modified init")
},
});
};
现在为了更好的表示,我在 QuickAddListView
和 ListView
中添加了控制台日志,它们是使用 _super
.
调用的 parent
所以如果我 运行 这样,我会得到这些打印件:
'ListView init' // This is parent of QuickAddListView
'QuickAddListView Init'
'QuickAddListView modified init'
构造函数的顺序是这样 View
-> ListView
-> QuickAddListView
所以所有这些都按应有的方式打印,但我想要修改 init
以直接调用 ListView
并跳过 QuickAddListView
原始 init
。
所以在那之后它应该只打印这个(意味着没有调用原始的 QuickAddListView init):
'ListView init' // This is parent of QuickAddListView
'QuickAddListView modified init'
在 javascript 中有没有办法指定您要调用的确切 parent?因此,不是调用一切都在链中,而是从您指定的位置开始(就像我的情况从 ListView
)?
例如在Python中你可以这样做:
from some_module import SomeBaseClass
class CustomBase(SomeBaseClass):
def m1(self):
super(CustomBase, self).m1()
class Custom(CustomBase):
def m1(self):
# skip CustomBase
super(CustomBase, self).m1()
Is there a way in javascript to specify exact parent you want to call?
是的,您几乎已经这样做了:使用 this._super
明确引用 QuickAddListView
的 init
方法。
So instead of calling that everything is in a chain, it would start from where you specify? And directly call ListView
and skip QuickAddListView
original init
.
对于这种情况,您只需更换行
this._super.apply(this, arguments);
来自
instance.web.ListView.prototype.init.apply(this, arguments);
(或者您可以访问 class,不确定 Odoo)
但是请注意,这是一个绝对的反模式。如果你想从 QuickAddListView
继承,你应该 运行 它的构造函数(或 init
方法)以便它可以初始化它需要的属性。如果你出于某种原因不想要它,你可能不应该继承它,而应该直接从 ListView
继承。
我已经在 Odoo 中定义了当前自定义 javascript 视图的扩展:
openerp.account_move_journal_test = function(instance){
var _t = instance.web._t,
_lt = instance.web._lt;
var QWeb = instance.web.qweb;
instance.web.account.QuickAddListView.include({
init: function(){
this._super.apply(this, arguments);
console.log("QuickAddListView modified init")
},
});
};
现在为了更好的表示,我在 QuickAddListView
和 ListView
中添加了控制台日志,它们是使用 _super
.
所以如果我 运行 这样,我会得到这些打印件:
'ListView init' // This is parent of QuickAddListView
'QuickAddListView Init'
'QuickAddListView modified init'
构造函数的顺序是这样 View
-> ListView
-> QuickAddListView
所以所有这些都按应有的方式打印,但我想要修改 init
以直接调用 ListView
并跳过 QuickAddListView
原始 init
。
所以在那之后它应该只打印这个(意味着没有调用原始的 QuickAddListView init):
'ListView init' // This is parent of QuickAddListView
'QuickAddListView modified init'
在 javascript 中有没有办法指定您要调用的确切 parent?因此,不是调用一切都在链中,而是从您指定的位置开始(就像我的情况从 ListView
)?
例如在Python中你可以这样做:
from some_module import SomeBaseClass
class CustomBase(SomeBaseClass):
def m1(self):
super(CustomBase, self).m1()
class Custom(CustomBase):
def m1(self):
# skip CustomBase
super(CustomBase, self).m1()
Is there a way in javascript to specify exact parent you want to call?
是的,您几乎已经这样做了:使用 this._super
明确引用 QuickAddListView
的 init
方法。
So instead of calling that everything is in a chain, it would start from where you specify? And directly call
ListView
and skipQuickAddListView
originalinit
.
对于这种情况,您只需更换行
this._super.apply(this, arguments);
来自
instance.web.ListView.prototype.init.apply(this, arguments);
(或者您可以访问 class,不确定 Odoo)
但是请注意,这是一个绝对的反模式。如果你想从 QuickAddListView
继承,你应该 运行 它的构造函数(或 init
方法)以便它可以初始化它需要的属性。如果你出于某种原因不想要它,你可能不应该继承它,而应该直接从 ListView
继承。