Javascript - call super on parents parent?

☆樱花仙子☆ 提交于 2020-01-04 05:30:31

问题


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 skip QuickAddListView original init.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!