Javascript-在父级父级上调用超级吗? [英] Javascript - call super on parents parent?

查看:95
本文介绍了Javascript-在父级父级上调用超级吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Odoo中定义了当前自定义javascript视图的扩展名:

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")
        },
    });    
};

现在为了更好地表示,我在QuickAddListViewListView中添加了控制台日志,这是使用_super调用的父级.

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'

构造函数的顺序是View-> ListView-> QuickAddListView

The order of constructors is this View -> ListView -> QuickAddListView

所以所有这些都按应有的方式打印,但是我想要的是修改init的方式,使其可以直接调用ListView并跳过QuickAddListView原始的init.

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.

因此,在此之后,它应该仅打印此内容(这意味着未调用原始QuickAddListView初始化):

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'

javascript中是否有一种方法可以指定您要调用的确切父对象?因此,与其说一切都在一个链条中,不如说是从您指定的位置开始(例如,我来自ListView)?

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)?

例如,在Python中,您可以执行以下操作:

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() 

推荐答案

javascript中是否有一种方法可以指定要调用的确切父级?

Is there a way in javascript to specify exact parent you want to call?

是的,您已经在这样做了:使用this._super显式引用QuickAddListViewinit方法.

Yes, you're pretty much already doing that: Using this._super to explicitly refer to QuickAddListView's init method.

因此,不是将所有内容都链接在一起,而是从您指定的位置开始?并直接调用ListView并跳过QuickAddListView原始init.

在这种情况下,您只需要替换行

For that case, you'd only have to replace the line

this._super.apply(this, arguments);

作者

instance.web.ListView.prototype.init.apply(this, arguments);

(或者您可以访问该类,不确定Odoo)

(or however you can access that class, not sure about Odoo)

但是请注意,这是绝对反模式.如果要从QuickAddListView继承,则应运行其构造函数(或init方法),以便它可以初始化所需的属性.如果出于某种原因不希望这样做,则可能不应该直接继承它,而应该直接从ListView继承.

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.

这篇关于Javascript-在父级父级上调用超级吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆