覆盖 Extjs 类并调用 callParent [英] Overriding Extjs classes and invoking callParent

查看:20
本文介绍了覆盖 Extjs 类并调用 callParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个月的 Extjs Web 应用程序开发经验.我遇到了这个问题:

I have a few months of experience developing Extjs web application. I ran into this problem:

当我覆盖一个类时,我修改了该方法并遵循了之前的实现并调用了callParent().覆盖部分有效,但 callParent() 调用了旧的实现.

When I override a class, I modified the method and followed the previous implementation and invoke callParent(). The overriding part works but the callParent() invoked the old implementation.

我的覆盖代码

Ext.override(Ext.layout.component.Draw, {
    finishedLayout: function (ownerContext) {

        console.log("new layouter being overriden");
        this.callParent(arguments);
    }
});

要覆盖的 Extjs 类方法:

The Extjs class method to be overridden:

finishedLayout: function (ownerContext) {
    var props = ownerContext.props,
        paddingInfo = ownerContext.getPaddingInfo();

    console.log("old layouter being overriden");
    this.owner.setSurfaceSize(props.contentWidth - paddingInfo.width, props.contentHeight - paddingInfo.height);

    this.callParent(arguments);
}

在控制台中,我可以看到新的布局器首先打印出消息,然后是旧的布局器实现......我放置了一个断点并回溯调用堆栈,callParent()新布局器称为旧布局器.我需要调用父类,而不是被覆盖的方法.

In the console, I can see that first the new layouter prints out the message followed by the old layouter implementation... I put a breakpoint and retrace the invocation stack, the callParent() of the new layouter called the old one. I need to call the parent class, but not the overridden method.

知道如何解决这个问题吗?

Any idea how to solve this problem?

推荐答案

如果您使用的是 ExtJS 4.1.3 或更高版本,您可以使用 this.callSuper(arguments) 来跳过"覆盖的方法并调用超类实现.

If you're using ExtJS 4.1.3 or later you can use this.callSuper(arguments) to "skip" the overridden method and call the superclass implementation.

ExtJS 文档 为方法提供了这个例子:

The ExtJS documentation for the method provides this example:

Ext.define('Ext.some.Class', {
    method: function () {
        console.log('Good');
    }
});

Ext.define('Ext.some.DerivedClass', {
    method: function () {
        console.log('Bad');

        // ... logic but with a bug ...

        this.callParent();
    }
});

Ext.define('App.patches.DerivedClass', {
    override: 'Ext.some.DerivedClass',

    method: function () {
        console.log('Fixed');

        // ... logic but with bug fixed ...

        this.callSuper();
    }
});

和评论:

patch 方法不能使用 callParent 来调用超类方法,因为这会调用包含错误的重写方法.换句话说,上面的补丁只会在控制台日志中产生Fixed"然后Good",而使用 callParent 会产生Fixed"然后Bad"然后Good"

The patch method cannot use callParent to call the superclass method since that would call the overridden method containing the bug. In other words, the above patch would only produce "Fixed" then "Good" in the console log, whereas, using callParent would produce "Fixed" then "Bad" then "Good"

这篇关于覆盖 Extjs 类并调用 callParent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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