问题
I have defined this extension of current custom javascript view in Odoo:
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")
},
});
};
Now for better representation I added console log in QuickAddListView
and in ListView
, which are parents that are called using _super
.
So if I run it like this, I get these prints:
'ListView init' // This is parent of QuickAddListView
'QuickAddListView Init'
'QuickAddListView modified init'
The order of constructors is this View
-> ListView
-> QuickAddListView
So all of this is printed like it should, but what I want is to modify init
in a way that it would directly call ListView
and skip QuickAddListView
original init
.
So after that it should print only this (meaning original QuickAddListView init was not called):
'ListView init' // This is parent of QuickAddListView
'QuickAddListView modified init'
Is there a way in javascript to specify exact parent you want to call? So instead of calling that everything is in a chain, it would start from where you specify (Like in my case from ListView
)?
For example in Python you can do something like this:
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()
回答1:
Is there a way in javascript to specify exact parent you want to call?
Yes, you're pretty much already doing that: Using this._super
to explicitly refer to QuickAddListView
's init
method.
So instead of calling that everything is in a chain, it would start from where you specify? And directly call
ListView
and skipQuickAddListView
originalinit
.
For that case, you'd only have to replace the line
this._super.apply(this, arguments);
by
instance.web.ListView.prototype.init.apply(this, arguments);
(or however you can access that class, not sure about Odoo)
But be warned that this an absolute antipattern. If you want to inherit from QuickAddListView
, you should run its constructor (or init
method) so that it can initialise the properties it needs. If you don't want that for whatever reason, you probably just should not inherit from it but inherit from ListView
directly.
来源:https://stackoverflow.com/questions/36370790/javascript-call-super-on-parents-parent